我正在尝试向设备发送 UDP 命令并从同一设备接收 UDP 响应。发送工作正常。我可以看到数据报离开(通过 WireShark)。我还可以看到从设备返回的数据报(同样,通过 WireShark)。命令离开和响应接收之间的周转时间约为 15 毫秒。
代码
Byte[] button_click(Byte[] command)
{
// Device exists at a particular IP address and listens for UDP commands on a particular port
IPEndPoint SendingEndpoint = new IPEndPoint(DEVICE_IP, DEVICE_PORT);
// Device always sends from port 32795 to whatever port the command originated from on my machine
IPEndPoint ReceivingEndpoint = new IPEndPoint(DEVICE_IP, 32795);
// Sending client
sendingClient = new UdpClient();
sendingClient.Connect(SendingEndpoint);
// Receiving client
receivingClient = new UdpClient();
receivingClient.Client.ReceiveTimeout = RECEIVE_TIMEOUT; // timeout after 4 seconds
receivingClient.Connect(receivingEndpoint);
// Send command and wait for response
Byte[] response = null;
try
{
sendingClient.Connect(DEVICE_IP, DEVICE_PORT);
sendingClient.Send(command, command.Length);
response = receivingClient.Receive(ref receivingEndpoint);
}
catch (SocketException e)
{
// If we timeout, discard SocketException and return null response
}
return response;
}
问题
我无法在我的应用程序中捕获收到的数据报。当我运行上面的代码时,我得到以下异常:
“连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。”
StackOverflow 上有类似的帖子,但似乎都没有解决我的情况。而且我已经确认我的数据包没有在我的防火墙中被清除。
我究竟做错了什么?
慕哥6287543
白猪掌柜的
相关分类