更新时间:2022-10-25 09:59:16
你没有检查连接(返回)状态。
您可能无法连接;或者不等待足够长的时间。
但是既然你实际上忽略了任何错误(空的前处理程序),你就会想知道发生了什么
I want status update from the devices which communicate on udp, every minute. what method i should use. Any kind of suggestions will be appreciated. Thanks
What I have tried:
I have tried this
foreach (IPAddress ips in iPAddresses) { Byte[] receiveBytes = { }; Byte[] dataToSend = new Byte[] { 0x8B, 0xB9, 0x00, 0x03, 0x05, 0x01, 0x09 }; IPEndPoint endPoint = new IPEndPoint(ips, 1024); EndPoint ep = (EndPoint)endPoint; Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { udpClient.Connect(endPoint); udpClient.SendTo(dataToSend, ep); // Thread.Sleep(50); IPEndPoint localip = new IPEndPoint(IPAddress.Any, 1200); UdpClient receivingUdpClient = new UdpClient(localip); receiveBytes = receivingUdpClient.Receive(ref endPoint); // Thread.Sleep(50); udpClient.Close(); receivingUdpClient.Close(); receivingUdpClient.Dispose(); updateData(receiveBytes, ips); } finally { udpClient.Dispose(); } }
The problem I can find is through this line-
receiveBytes = receivingUdpClient.Receive(ref endPoint);
ref endpoint in receive() is having 1st ip address only. its not changing. Send method is able to change ip address. but not receive method.
I have tried some other methods too before this. but this is what I am doing now
You're not checking the "connect" (return) status.
You're probably failing to connect; or not "waiting" long enough.
But since you're effectively ignoring any errors ("empty" ex handler), you're left to wonder what's going on.