UDP transmission over WAN

Stephen

New Member
#1
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
Mã:
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
Mã:
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
Mã:
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);
        }
    }
 

Admin

Administrator
Thành viên BQT
#2
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?
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top