0

I have asked a couple of similar questions the last couple of days and received some really great help. I now understand my problem quite a bit better but I appear to have hit a snag. I have written a client server application that uses both a TCP and UDP connection. The TCP connection works fine over both LAN and WAN but the UDP connection fails over WAN. Based on the questions I asked previously I realized that my server needed to reply to the client at the EndPoint from which it received a communication. I set everything up to work that way. I will post code after the question. My problem is now that while I am using the EndPoint from the client connection and the client is establishing connection first I am still unable to make the UDP connection. It appeared to work over one network but then failed over all the others I have tried. Any help on figuring this out is appreciated. Here is the code.

Receive UDP Messages on the server

private void receiveUDP()
    {
        System.Net.IPEndPoint test = new System.Net.IPEndPoint(System.Net.IPAddress.Any,UDP_PORT);
        System.Net.EndPoint serverIP = (System.Net.EndPoint)test;
        trans.Bind(serverIP);
        System.Net.IPEndPoint ipep = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
        System.Net.EndPoint Remote = (System.Net.EndPoint)ipep;

        while (true)
        {

            byte[] content = new byte[1024];

            int recv = trans.ReceiveFrom(content,ref Remote);

            int portNum = ((System.Net.IPEndPoint)Remote).Port;
            string message = Encoding.ASCII.GetString(content);
            string[] data = message.Split((char)124);
            //UpdateStatus(data[0] + data[1]);

            UserConnection sender = (UserConnection)clients[data[0]];
            if (sender.PortNumber != portNum)
                sender.PortNumber = portNum;
            if (sender.RemoteEnd != Remote)
            {
                sender.RemoteEnd = Remote;//Stores the EndPoint from the client connection
            }
            if (data.Length > 2)
            {
                OnLineRecieved(sender, data[1] + "|" + data[2]);
            }
            else
            {
                OnLineRecieved(sender, data[1]);
            }
        }
    }

Client Listens here

private void receiveUDP()
    {
        System.Net.IPEndPoint test = new System.Net.IPEndPoint(System.Net.IPAddress.Any,UDP_PORT_NUMBER);
        System.Net.EndPoint serverIP = (System.Net.EndPoint)test;
        server.Bind(serverIP);
        server.Ttl = 50;

        EndPoint RemoteServ = (EndPoint)servIP;
        while (true)
        {
            byte[] content = new byte[1024];
            int  data = server.ReceiveFrom(content, ref RemoteServ);

            string message = Encoding.ASCII.GetString(content);
            result = message;

            ProcessCommands(message);

        }
    }

EDIT: SERVER'S Sending function

public void SendData(string data)
    {
        if (RemoteEnd != null)//RemoteEnd is refreshed every time the client sends a UDP message
//Each Clients RemoteEnd is stored in a collection of Client objects in a server hashtable
        {
            //ipep = new IPEndPoint(ipAdd, PortNumber);
            byte[] dataArr = Encoding.ASCII.GetBytes(data);

            trans.SendTo(dataArr, dataArr.Length, SocketFlags.None, RemoteEnd);
        }
    }
Stephen
  • 174
  • 4
  • 14
  • 2
    Are you using a router? If so, your server must be "visible" to clients outside of your LAN in some way (probably by being added to the DMZ of your router). – Kiril Aug 13 '11 at 15:07
  • @Lirik The client is actually able to communicate with the server but the server cannot reply to any of those communications. – Stephen Aug 13 '11 at 17:33
  • 1
    I don't see your server's sending code... can you please post some code depicting what your server is doing to send data? – Kiril Aug 13 '11 at 18:10
  • @Lirik Edited the original post to include the code the server uses to send data. – Stephen Aug 13 '11 at 18:34

2 Answers2

1

There could be a lot of things wrong. Remember, UDP doesn't provide transmit pacing, retransmissions, or acknowledgements. So if you need them, you must provide them. If you have the client send first and then wait for responses to each query, your first lost packet will kill the connection.

You also kind of forgot to describe the problem. You say you fail to make the connection, but what does that mean? Does the server receive the client's first packet or not? Does the client receive the server's first reply or not?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Sorry I didnt explain the problem clear enough. The client sends data first. The server receives the data just fine because the server has the necessary ports forwarded. However, the client does not receive any replies. I am not having the client listening for a reply to every message. Instead it listens for any reply at all anytime. The client does not receive any return messages at all. – Stephen Aug 13 '11 at 16:25
  • 1
    Does the server send using the very same socket it received on? Can you use 'tcpdump' to catch the server receiving the UDP packet and sending a UDP packet and paste them here? (Compare the packets, the source IP/port of one should be the destination of the other and vice-versa, exactly.) – David Schwartz Aug 13 '11 at 16:27
  • Yeah I will do that and post the results asap. – Stephen Aug 13 '11 at 16:30
  • Would you be capable of looking at this information over a chat or in a different medium? I am not quite comfortable posting public IP addresses associated with myself and my business in forums. – Stephen Aug 13 '11 at 17:30
1

You have to determine if this is a programming problem or a network configuration problem.

what I would do is run the client app on the server machine and the server app on the client machine (and switch the hosts they connect to).

If the server app no longer receives UDP messages from the client app, then you have a networking configuration problem.

If the server app can still receive messages from the client app, then you have a programming problem.

rbp
  • 1,850
  • 15
  • 28