UDP中继实现不像我预期的那样工作

时间:2015-09-01 12:50:12

标签: c# unity3d udp

我正在尝试实现UDP通信的中继。

例如,我有两个终端:

  • 192.168.10.5
  • 192.168.10.6

我想通过192.168.10.3监视两者之间。

我的环境:

Unity 5.1.2-f1 on MacOS X 10.8.5 using C#

以下是我与UDP通信相关的代码段。

void Monitor() {
        UdpClient client = new UdpClient (port);
        client.Client.ReceiveTimeout = 1000; // msec
        client.Client.Blocking = false;

        while (ToggleComm.isOn) {
            try {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP);
                string text = Encoding.ASCII.GetString(data);

                if (text.Length == 0) {
                    Thread.Sleep(20);
                    continue;
                }
                string fromIP = anyIP.Address.ToString();
                // send to the other
                if (fromIP.Equals(ipadr1)) {
                    client.Send(data, data.Length, ipadr2, port);
                    Debug.Log("from: " + ipadr1 + " to " + ipadr2 + data);
                } else {
                    client.Send(data, data.Length, ipadr1, port);
                    Debug.Log("from: " + ipadr2 + " to " + ipadr1 + data);
                }
            }
            catch (Exception err) {

            }
            // without this sleep, on android, the app will freeze at Unity splash screen
            Thread.Sleep(200);
        }
        client.Close ();
    }

使用上面的代码,

  • 我可以从192.168.10.5发送到192.168.10.3
  • 我可以从192.168.10.3发送到192.168.10.6(中继)
  • 我可以从192.168.10.6
  • 收到192.168.10.3
  • 我无法从192.168.10.3
  • 收到192.168.10.5

其中192.168.10.6作为echo服务器工作,返回接收到的字符串。

我在继电器上的调试打印说

  • 从:192.168.10.5到192.168.10.6
  • from:192.168.10.6 to 192.168.10.5

似乎中继(192.168.10.3)发送到192.168.10.5,但192.168.10.5无法接收中继的。

1 个答案:

答案 0 :(得分:0)

以下代码工作(虽然不优雅)。

我误解了发件人和收件人的端口。

void Monitor() {
    UdpClient client = new UdpClient (port);
    client.Client.ReceiveTimeout = 300; // msec
    client.Client.Blocking = false;

    string fromPort = "6000"; // at first 6000 is set as dummy
    while (ToggleComm.isOn) {
        try {
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = client.Receive(ref anyIP);
            string text = Encoding.ASCII.GetString(data);

            if (text.Length == 0) {
                Thread.Sleep(20);
                continue;
            }
            string fromIP = anyIP.Address.ToString();
            // send to the other
            if (fromIP.Equals(ipadr1)) {
                fromPort = anyIP.Port.ToString(); // store the port 
                client.Send(data, data.Length, ipadr2, port);
                Debug.Log("1 from: " + fromIP + "(" + fromPort
                          + ") to " + ipadr2 + "(" + port.ToString() + ")");
            } else {
                client.Send(data, data.Length, ipadr1, Convert.ToInt32(fromPort));

                Debug.Log("2 from: " + fromIP + "(" + "..." 
                          + ") to " + ipadr1 + "(" + fromPort + ")");
            }
        }
        catch (Exception err) {

        }
        // without this sleep, on android, the app will freeze at Unity splash screen
        Thread.Sleep(200);
    }
    client.Close ();
}

我把团结项目放在github (v0.3)