UDP - 发送多播消息并侦听响应会出现 SocketException

发布于 2024-11-14 09:05:14 字数 1816 浏览 7 评论 0原文

我正在发送 UDP 多播消息来查找网络上的某些设备。然后,我在端口 5001 上侦听响应。

我的工作流程如下:

  1. 将多播“查找”消息发送到本地子网。
  2. 网络上的任何 wiznet 设备都会响应我想要接收的信息包

一切正常在 Windows XP 上没问题,但在 Windows 7 上,我收到 SocketException: * 类型的异常

通常只允许每个套接字地址(协议/网络地址/端口)使用一次

我可以看到多播消息在 Wireshark,我看到设备的响应,但我的代码没有响应。我的代码如下所示:

  public void StartListen()
  {
      SendFind();
      try {
          IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 0);
          UdpClient listenClient = new UdpClient(5001);

          UdpState s = new UdpState();
          s.endpoint = localEp;
          s.client = listenClient;

          //allow time for the find to work - aka clutching at straws
          Thread.Sleep(500);

          while (listenClient.Available > 0)
          {
              listenClient.BeginReceive(ReceiveCallback, s);
              Thread.Sleep(500);
          }
      }

      catch (SocketException e)
      {
          Trace.WriteLine("Could not bind to socket on " + _localPort);
      }

      listenClient.Close();
  }

.. 和 RecieveCallBack ..

private void ReceiveCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).client;
    IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).endpoint;
    Byte[] receiveBytes = u.EndReceive(ar, ref e);
    rxByteList.Add(receiveBytes);
    messageRxed = true;
}

** 更新 **

所以我尝试了构建此代码的各种方法。看来问题与不同 UdpClient 上的发送和接收有关。我的异常是由于创建一个 UdpClient 在打开一个发送后立即接收而引起的 - 在发送和接收之间添加延迟修复了此问题。

我已更改代码以使用相同的 UdpClient 进行发送和接收,但我仍然没有收到任何接收信息。

I'm sending a UDP multicast message to find certain devices on the network. I then listen for a response on port 5001.

My workflow is as follows:

  1. Send a multicast "find" message to the local subnet.
  2. Any wiznet devices on the network respond with an info packet which I want to receive

It all works fine on Windows XP, but on Windows 7, I get an exception of type SocketException: *

Only one usage of each socket address (protocol/network address/port) is normally permitted

I can see the multicast message go out in Wireshark, and I see the response from the device(s), but my code doesn't respond. My code looks like this:

  public void StartListen()
  {
      SendFind();
      try {
          IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 0);
          UdpClient listenClient = new UdpClient(5001);

          UdpState s = new UdpState();
          s.endpoint = localEp;
          s.client = listenClient;

          //allow time for the find to work - aka clutching at straws
          Thread.Sleep(500);

          while (listenClient.Available > 0)
          {
              listenClient.BeginReceive(ReceiveCallback, s);
              Thread.Sleep(500);
          }
      }

      catch (SocketException e)
      {
          Trace.WriteLine("Could not bind to socket on " + _localPort);
      }

      listenClient.Close();
  }

.. and RecieveCallBack ..

private void ReceiveCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).client;
    IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).endpoint;
    Byte[] receiveBytes = u.EndReceive(ar, ref e);
    rxByteList.Add(receiveBytes);
    messageRxed = true;
}

** UPDATE **

So I have tried various ways of structuring this code. It seems that the problem is related to sending and receiving on different UdpClients. My exception was caused by creating a UdpClient to recieve immediately after opening one to send - adding a delay between the send and receive fixed this.

I have altered my code to use the same UdpClient for the send and receive, but I'm still not getting anything on the receive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2024-11-21 09:05:14

已排序!这是因为我的 Sendpacket 使用“udpclient.connect”,它将接收限制为提供给连接方法的端点中的所有数据。

Sorted! It was because my Sendpacket used "udpclient.connect", which restricts the receive to all data in the endpoint supplied to the connect method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文