UDP 中继实施未按预期工作
UDP relay implementation do not work as I expected
我正在尝试实现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.3 从 192.168.10.6 接收
- 我无法在 192.168.10.5 从 192.168.10.3 接收
其中 192.168.10.6 作为回显服务器,返回接收到的字符串。
我在继电器上的调试打印显示
- 从:192.168.10.5 到 192.168.10.6
- 从:192.168.10.6 到 192.168.10.5
似乎中继(192.168.10.3)发送到192.168.10.5,但是192.168.10.5收不到中继的
以下代码有效(虽然还不够优雅)。
我对发送方和接收方的端口有误解
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 ();
}
我把unity项目放在github (v0.3)
我正在尝试实现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.3 从 192.168.10.6 接收
- 我无法在 192.168.10.5 从 192.168.10.3 接收
其中 192.168.10.6 作为回显服务器,返回接收到的字符串。
我在继电器上的调试打印显示
- 从:192.168.10.5 到 192.168.10.6
- 从:192.168.10.6 到 192.168.10.5
似乎中继(192.168.10.3)发送到192.168.10.5,但是192.168.10.5收不到中继的
以下代码有效(虽然还不够优雅)。
我对发送方和接收方的端口有误解
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 ();
}
我把unity项目放在github (v0.3)