2012-02-22 169 views
1

Iam尝试从我的客户端向我的服务器发送消息(通过UDP)。服务器应该回答这个消息,如果客户端收到这个答案,他应该打印出一条消息。C#UDP服务器/客户端 - NAT

如果我运行我的本地网络上的客户端和服务器一切正常。 如果我尝试通过互联网从另一台PC连接到我的网络外,服务器收到客户端请求,发回答案,但客户端永远不会收到此答案。客户端和服务器都在NAT后面,但我在服务器的NAT上转发了这些端口,并且服务器拥有了自己的DNS。我已经尝试过NAT遍历,但在接收到客户端请求后,它给了我与服务器的IPEndPoint相同的IP和端口地址。

我不知道如何解决这个问题,所以任何指导都将不胜感激。

客户

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

} 

的server's代码几乎是相同的:

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

} 
+0

不确定,但您可以检查防火墙设置 – 2012-02-22 11:46:36

+0

关闭客户端和服务器上的所有防火墙以消除潜在问题。如果您的客户端向服务器发送消息,并且服务器接收到该消息,则客户端肯定会收到服务器的答复。除了第三方防火墙之外,您还可以使用Windows防火墙。尝试打开客户端和服务器路由器的DMZ /端口转发。如果它仍然不起作用,我不太确定它可能是什么。 – Jason 2012-02-24 03:41:12

回答

0

我相信这是一个端口cofniguration问题,

8080几乎是可能被配置为备用http

“你用来接收数据的UdpClient agrams必须使用多播端口号”from MSDN

希望这有助于和好运

克里希纳

+0

我会试试,非常感谢 – Fuzzyl0gic171 2012-02-22 15:48:14

+0

不幸的是它仍然没有工作 – Fuzzyl0gic171 2012-02-23 23:10:14

+0

8080可能被任何东西使用 - 但如果你成功地绑定它 - 它可以使用。您的MSDN引用不包括“如果您打算接收组播数据报...”。 – markmnl 2013-09-11 02:42:06

0

你不需要做任何事情unordinary穿越NAT在你描述的设置来创建的,你只是需要将它从服务器发回给客户端;具体为:您必须发送回终点,即IP 端口,您已收到它。

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