开发者

UDP transmission over WAN

开发者 https://www.devze.com 2023-03-27 17:28 出处:网络
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 c

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);
        }
    }


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消