问题描述
我正在一组主机上进行编程,这些主机在udp套接字上相互通信。在每个主机上仅创建一个C#UdpClient对象和任务来接收和发送消息。当UdpClient收到广播消息时,它将连接到另一台主机进行答复,然后继续接收。但是在收到并处理了第一条消息后,就没有第二条消息了。我已经检查了发件人是否可以返回。连接/发送后,C#UdpClient是否不能再次用作侦听器?
UdpClient udpReceiver = new UdpClient(new IPEndPoint(IPAddress.Any,0));
IPEndPoint endpoint = (IPEndPoint)udpReceiver.Client.LocalEndPoint;
_log.InfoFormat("Listening at {0}:{1} ...",endpoint.Address,endpoint.Port);
MessageTask = Task.Run(() =>
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any,0);
string msg;
byte[] buffer;
while (true)
{
buffer = udpReceiver.Receive(ref remoteIpEndPoint);
msg = Encoding.ASCII.GetString(buffer);
ProcessMessage(msg); ---> Connect/Send to another host
buffer = null;
}
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)