I made two simple client and server socket chat programs.

Client connects to and interacts with the server application perfectly on localhost, but I can't get the client to connect over directly connected LAN.

The client/server are connecting to/listening on port 2869 - the only port I found that would allow the server machine to be telnet'd into. No PC firewalls on either machine or routers that could possibly block connection.

Any insights as to why my client machine won't connect to a server machine over a local network?

Recommended Answers

All 8 Replies

Please post the code you have for listening and connecting over LAN.

commented: helpful +2

Are you using synchronous or asynchronous sockets?

Either way, if you have a socket and have no mechanism to loop (with a timer or something else) a read on the socket, the server and client will connect and basically sit there without listening to each other. You can talk all you want on one socket and send 10000 messages, but if the other end doesnt "read" the socket, it just sits there.

commented: helpful +2

Async, Udp.
// Server, listening

private void Form1_Load(object sender, EventArgs e) {            
    try {
        //using UDP sockets

        serverSocket = new Socket(AddressFamily.InterNetwork, 
            SocketType.Dgram, ProtocolType.Udp);

        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1000);

        //Bind this address to the server
        serverSocket.Bind(ipeServer);
        
        IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);

        EndPoint epSender = (EndPoint) ipeSender;

        //Start receiving data
        serverSocket.BeginReceiveFrom (byteData, 0, byteData.Length, 
            SocketFlags.None, ref epSender, 
               new AsyncCallback(OnReceive), epSender);
    }
    catch (Exception ex) 
    { 
        //Error
    }
}

//Client, connecting

try {
    //Using UDP sockets
    clientSocket = new Socket(AddressFamily.InterNetwork, 
        SocketType.Dgram, ProtocolType.Udp);

    //Get IP address of the server machine from input
    IPAddress ipAddress = IPAddress.Parse(txtServerIP.Text);

    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

    epServer = (EndPoint)ipEndPoint;
    
    Data msgToSend = new Data ();
    msgToSend.cmdCommand = Command.Login;
    msgToSend.strMessage = null;
    msgToSend.strName = strName;

    byte[] byteData = msgToSend.ToByte();
    
    //Login to the server
    clientSocket.BeginSendTo(byteData, 0, byteData.Length, 
        SocketFlags.None, epServer, 
        new AsyncCallback(OnSend), null);
}
catch (Exception ex)
{ 
    //Error
}

Like I said, works fine on localhost/loopback with multiple clients, but when trying over ethernet with ping-able IP's, client never seems to connect.

I've never used Udp, but I still think you need to have your server "listen" for connections.

Maybe this will help: Socket.Listen Method (MSDN)

Thanks for replying, but that's what the server code above is for. In a continuing loop, it checks for any data on the stream. Udp servers don't listen for incoming clients, they just look for any data coming from other clients.


What's the relationship between testing locally and testing over ethernet? I don't understand why the server will listen for and allow incoming clients to connect over localhost, yet not between two directly connected PC's.

Oh, my bad. Like I said, I've never used Udp.

On a local/loopback connection you said it's working...the only thing I can think of is a router, either at the server end or the client end is dropping the data, or doesn't know where to route it so it drops it.

You said there are no firewalls blocking it, so maybe it's just a routing issue? I'm grasping here...no real idea, but throwing out suggestions.

Sorry I'm not much help, maybe someone else might be.

Have you tried using a program that you haven't written yourself that uses Udp to see if it works? Can the client and server "ping" each other with the command line console?

Good luck :)

Just two PC's by cable, so no firewall or routing issues. They can ping, telnet (only on one port though), and see each other.

That's a good idea though, I'll download a similar program as a means of narrowing down the problem.

The client/server are connecting to/listening on port 2869 - the only port I found that would allow the server machine to be telnet'd into. No PC firewalls on either machine or routers that could possibly block connection.

I just noticed this. If something else is already listening on this port, then it won't work.

But this is in contradiction to your code, which sets port 1000. Are you absolutely sure that the firewall on both machines is disabled?

If it works locally but not over LAN, it really does fall down into the following problems;

1. A firewall is turned on
2. Packets are being lost
3. You are not listening to the right adapter

I can't see anything immediately obvious that is wrong with your code.

Out of interest, why aren't you using TCP?

EDIT: Just to eliminate the configuration of your ad-hoc network, put a switch in the middle between the computers and set them to manu IP Addresses of say 10.0.0.1 and 10.0.0.2 and see if it still fails.

Patch cable ad-hoc networks are terribly unreliable in my experience

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.