ThaMe90
ThaMe90

Reputation: 4296

Unable to connect to other instances of the same program via TCP

I know this might have been asked a thousand times before, but I can't seem to find any specific information about my case.

I have a C# client program that has to connect to other instances of the client over a LAN. To connect one client to another, I use a TcpListener/TcpClient aproach. Both instances have a listener and are able to create a new client to connect/listen to one another (it is independant of which instance started the connection).

To create the listener, I use the following bit of code:

// In the constructor:
listener = new TcpListener(IPAddress.Any, 32842);
listenThread = new Thread((ThreadStart)ListenForConnections);
listenThread.Name = "ListenThread";
listenThread.IsBackground = true;
listenThread.Start();

// Listening for connections:
private void ListenForConnections()
{
    listener.Start();
    Console.WriteLine("Started listening for connections");
    for (; ; )
    {
        if (listener.Pending())
        {
            using (TcpClient client = listener.AcceptTcpClient())
            {
                // My own layer over the TcpClient.
                AsyncTCPClient other = new AsyncTCPClient(client);
                Console.WriteLine("Connection from " + client.Client.RemoteEndPoint);
                other.Received += DataReceived;
                other.Exception += ExceptionOccurred;
                connections.Add("Player", other);
                other.Start();
            }
        }
        else
        {
            Thread.Sleep(5);
        }
    }
}

To create and connect to another client, I use the following bit of code:

public void Connect(IPEndPoint other)
{
    if (socket == null)
    {
        socket = new TcpClient(AddressFamily.InterNetwork);
        socket.Client.ReceiveBufferSize = 2 * 1024 * 1024;
    }
    // Should force-close the socket after 5 seconds if it can't be closed automatically.
    socket.LingerState = new LingerOption(true, 5);
    socket.BeginConnect(other.Address, other.Port, ConnectionCallback, other);
    IsConnecting = true;
}

The ConnectionCallback given as a parameter to BeginConnect looks like this:

private void ConnectionCallback(IAsyncResult result)
{
    IsConnecting = false;
    IsConnected = socket.Connected;
    if (IsConnected)
    {
        IPEndPoint connectedTo = (IPEndPoint)result.AsyncState;
        stream = socket.GetStream();
        if (Connected != null)
        {
            Connected(this, null);
        }
    }
    else
    {
        if (Exception != null)
        {
            RaiseException(new Exception("Unable to connect to host"));
        }
    }
}

However, everytime I get to the callback, the TcpClient failed to connect to the other instance and the Exception event is thrown. Now what I've found while searching around the internet (Google) is that it might have something to do with a firewall on either sides of the connection. But I've tested it with all firewalls off, so this can't be that.

Upvotes: 3

Views: 285

Answers (1)

Rahul
Rahul

Reputation: 36

I don't see Socket.EndConnect() being called in the callback. See this in MSDN: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.endconnect.aspx

Upvotes: 2

Related Questions