1
votes

Iam trying to send a message (via UDP) from my client to my server. The server should answer this message and if the client receives this answer he should print out a message.

If i run the client and server on my local network everything works fine. If i try to connect through the internet from another PC outside my network the server receives the request of the client, sends an answer back, but the client never receives this answer. The client and the server are both behind a NAT but i portforwarded the ports at the server´s NAT and the server got its own DNS. I already tried NAT traversal but it gives me the same IP and port adress as the IPEndPoint of the server, after receiveing the request of the client, does.

I´ve got no idea how to fix this, so any guidance would be much appreciated.

Client

public static void Main()
{
    Thread receiveThread = new Thread(new ThreadStart(ReceiveData));
    receiveThread.Start();

    object[] oData = {1};
    sendData(oData, 0,0, "Li"); 

    while (true)
    {
        Console.ReadLine();
    }
}


private void receiveData()
{
    string receivePort = 8080;

    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    client.ReceiveTimeout = 1000;
    IPEndPoint end = new IPEndPoint(IPAddress.Any, receivePort);
    client.Bind(end);


    while (true)
    {
        try
        {
            byte[] data = new byte[1024];

            client.Receive(data, 0, data.Length, SocketFlags.None);

            object[] receivedObj = Deserialize(data);

            string sType = (string)receivedObj[3];

            if (sType == "Li")
            {
           console.WriteLine("received Li");
            }
         }
         catch (Exception err)
         {
                Console.WriteLine(err.ToString());
         }
     }
}

public static void sendData(object[] oData, int iFrom, int iTo, string sType)
{
    string sendPort = 17171;

    UdpClient client = new UdpClient();

    string IP = "ThisIsTheDNSofmyServer.com"; //ServerDNS
    //string IP = "192.168.xxx.xxx"; //serverIP in LAN 

    if (IP.StartsWith("T")) 
    {
        IP = (Dns.GetHostAddresses(IP))[0].ToString();
    }

    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), sendPort);

    oData[1] = iFrom;
    oData[2] = iTo;
    oData[3] = sType;

    Byte[] data = Serialize(oData);
    client.Send(data, data.Length, remoteEndPoint);

}

The server´s code is almost the same:

public static void Main()
{
    Thread receiveThread = new Thread(new ThreadStart(ReceiveData));
    receiveThread.Start();

    while (true)
    {
        Console.ReadLine();
    }
}

private static void ReceiveData()
{
    int receivePort = 17171;
    UdpClient client = new UdpClient(receivePort);

    while (true)
    {
        try
        {
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = new byte[1024];

            data = client.Receive(ref anyIP);

            object[] receivedObj = Deserialize(data);

            //if I receive data send an Answer
            sendData(receivedObj, 0,0,"Li",anyIP.Address.ToString()); 

        }
        catch (Exception err)
        {
            Console.WriteLine(err.ToString());
        }
    }
}

private static void sendData(object[] oData, int iFrom, int iTo, string sType, string IP)
{
    int sendPort = 8080;
    object[] paket = { oData, iFrom, iTo, sType };

    UdpClient client = new UdpClient();

    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), sendPort); 

    client.Send(data, data.Length, remoteEndPoint);

 }
2
not sure, but you may check the firewall settingsAli Hasan
Turn off all firewalls on the client and server to eliminate potential issues. If your client sends a message to the server, and the server receives it, the client should most definitely receive the server's reply. You may have Windows Firewall active in addition to a third-party firewall. Try turning on DMZ/port-forwarding for both client and server routers. If it still doesn't work, I'm not quite sure what it could be.Jason

2 Answers

0
votes

i believe this is a port cofniguration issue,

8080 is almost likely to be configured as alternate http

"The UdpClient you use to receive datagrams must be created using the multicast port number" from MSDN

Hope this helps and good luck

Krishna

0
votes

You do not need to do anything unordinary to traverse NAT in the setup you described, you just need to send it from the server back to your client; specifically: you must send back to the end point, i.e. IP and port, you received it on.

client.Send(data, data.Length, remoteEndPoint); // remoteEndPoint is the IPEndPoint you got the datagram on