简体   繁体   English

UdpClient在广播地址上接收

[英]UdpClient receive on broadcast address

In c# I am using the UdpClient.Receive function: 在c#中我使用的是UdpClient.Receive函数:

public void StartUdpListener(Object state)
    {
        try
        {
            udpServer = new UdpClient(new IPEndPoint(IPAddress.Broadcast, 1234));
        }
        catch (SocketException ex)
        {
            MessageBox.Show(ex.ErrorCode.ToString());
        }

        IPEndPoint remoteEndPoint = null;
        receivedNotification=udpServer.Receive(ref remoteEndPoint);
        ...

However I am getting a socket exception saying that the address is not available with error code 10049 What do I do to negate this exception? 但是我收到一个套接字异常,说该地址不可用,错误代码为10049我该怎么做才能否定这个异常?

Here's the jist of some code I am currently using in a production app that works (we've got a bit extra in there to handle the case where the client are server apps are running on a standalone installation). 以下是我目前在生产应用程序中使用的一些代码的写法(我们在那里有一些额外的东西来处理客户端是服务器应用程序在独立安装上运行的情况)。 It's job is to receive udp notifications that messages are ready for processing. 它的工作是接收消息准备好处理的udp通知。 As mentioned by Adam Alexander your only problem is that you need to use IPAddress.Any, instead of IPAddress.Broadcast. 正如Adam Alexander所提到的,唯一的问题是你需要使用IPAddress.Any而不是IPAddress.Broadcast。 You would only use IPAddress.Broadcast when you wanted to Send a broadcast UDP packet. 如果要发送广播UDP数据包,则只能使用IPAddress.Broadcast。

Set up the udp client 设置udp客户端

this.broadcastAddress = new IPEndPoint(IPAddress.Any, 1234);
this.udpClient = new UdpClient();
this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.udpClient.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

And to trigger the start of an async receive using a callback. 并使用回调触发异步接收的启动。

this.udpClient.Client.Bind(this.broadcastAddress);
this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);

Hopefully this helps, you should be able to adapt it to working synchronously without too much issue. 希望这有所帮助,您应该能够使其适应同步工作而不会出现太多问题。 Very similar to what you are doing. 与你正在做的非常相似。 If you're still getting the error after this then something else must be using the port that you are trying to listen on. 如果您在此之后仍然收到错误,那么其他内容必须使用您正在尝试收听的端口。

So, to clarify. 所以,澄清一下。

IPAddress.Any = Used to receive. IPAddress.Any =用于接收。 I want to listen for a packet arriving on any IP Address. 我想听一个到达任何IP地址的数据包。 IPAddress.Broadcast = Used to send. IPAddress.Broadcast =用于发送。 I want to send a packet to anyone who is listening. 我想向正在收听的人发送数据包。

for your purposes I believe you will want to use IPAddress.Any instead of IPAddress.Broadcast. 为了您的目的,我相信您将要使用IPAddress.Any而不是IPAddress.Broadcast。 Hope this helps! 希望这可以帮助!

That error means the protocol cant bind to the selected IP/port combination. 该错误意味着协议无法绑定到所选的IP /端口组合。

I havent used UDP broadcast in ages, but I do recall you need to use different IP ranges. 我没有使用UDP广播,但我记得你需要使用不同的IP范围。

There's nothing wrong with the way you have configured your UdpClient. 配置UdpClient的方式没有任何问题。 Have you tried a different port number? 你尝试过不同的端口号吗? Perhaps 1234 is already in use on your system by a different app. 也许1234已经在您的系统上由另一个应用程序使用。

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

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