繁体   English   中英

C# UDP 套接字绑定异常

[英]C# UDP Socket Bind Exception

我正在尝试为无线网状网络创建一个程序(除名称外都是临时的)。 大多数网络将处理 TCP 消息,但为了确定所有邻居 IP(因为它们在启动时是未知的),我选择使用 UDP 广播作为初始发现消息。 该代码在大多数情况下是无关紧要的,因为初始测试似乎有效,目前我不称之为。

下面是接收和注册邻居IP的部分。

protected override void ReceiveMessageUDP()
    {
        while (live)
        {
            try
            {
                UdpListener = new UdpClient(_PORTRECEIVE);
                UdpListener.EnableBroadcast = true;
                Socket socket = UdpListener.Client;
                IPEndPoint endPoint1 = new IPEndPoint(IPAddress.Loopback, _PORTRECEIVE);
                IPEndPoint endPoint2 = new IPEndPoint(IPAddress.Any, _PORTRECEIVE);
                socket.Bind(endPoint2);
                socket.Listen(25);
                Socket connected = socket.Accept();
                byte[] buffer = new byte[1024];
                int length = connected.Receive(buffer);

                IPAddress remoteIP = ((IPEndPoint)connected.RemoteEndPoint).Address;
                if (!ipAddresses.Contains(remoteIP))
                    ipAddresses.Add(remoteIP);

                Message.Add(Encoding.ASCII.GetString(buffer, 0, length));
            }
            catch (SocketException e) { Console.WriteLine(e.ErrorCode); }
            catch (Exception) { }
        }
    }

我已经用 IPEndPoints 进行了测试,无论我如何设置,绑定都会失败并显示 SocketException.ErrorCode 10022 Windows Socket Error Codes 这是一个无效的参数,但我对这意味着什么感到困惑,因为所需的参数是一个端点。

这在第一次运行时失败,所以这不像我试图重新绑定端口。

我同意史蒂夫。 但是,您声明

“UDP 不允许绑定或其他东西。”

但确实如此。 您可以将任何套接字(udp 与否)绑定到任何网络接口。 史蒂夫给出了实现它的提示。 这就是我的做法:

LocalEP = new IPEndPoint(<local IP>, _PORTRECEIVE) ;
listener = New UdpClient(LocalEP) ;

正如史蒂夫指出的那样,一旦 UdpClient 被实例化,它就不允许重新绑定到另一个接口。 诀窍是告诉构造函数事先绑定到你想要的接口。 替换为您要使用的本地接口的 IP 地址。

当然,这仅在您拥有(或可能拥有)多个网络接口并希望使用特定的网络接口时才有用。 否则,我已经看到带有有效网关的接口通常是客户端默认绑定的接口。 这通常是正确的。

罗伯托

在构建端口时,您已经将端口绑定到UdpCLient 然后您不能将同一端口绑定到单独的IPEndPoint

UdpListener = new UdpClient(_PORTRECEIVE);  // binds port to UDP client
...
IPEndPoint endPoint2 = new IPEndPoint(IPAddress.Any, _PORTRECEIVE);
socket.Bind(endPoint2);  // attempts to bind same port to end point

如果您想这样做,请构建并绑定IPEndPoint ,然后使用它而不是端口构建UdpClient

IPEndPoint endPoint2 = new IPEndPoint(IPAddress.Any, _PORTRECEIVE);
socket.Bind(endPoint2);  // bind port to end point

UdpListener = new UdpClient(endPoint2);  // binds port to UDP client via endpoint

我不确定您为什么还要在同一端口上设置另一个端点endPoint1 如果您尝试使用它,这可能会导致问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM