C# UdpClient 接收广播地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/746519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:35:56  来源:igfitidea点击:

UdpClient receive on broadcast address

c#udp

提问by Avik

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 我该怎么做才能否定这个异常?

采纳答案by Mark Allanson

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. As mentioned by Adam Alexander your only problem is that you need to use IPAddress.Any, instead of IPAddress.Broadcast. You would only use IPAddress.Broadcast when you wanted to Senda broadcast UDP packet.

这是我目前在一个有效的生产应用程序中使用的一些代码的 jist(我们有一些额外的东西来处理客户端是服务器应用程序在独立安装上运行的情况)。它的工作是接收消息已准备好进行处理的 udp 通知。正如 Adam Alexander 所提到的,您唯一的问题是您需要使用 IPAddress.Any,而不是 IPAddress.Broadcast。当您想发送广播 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. I want to listen for a packet arriving on any IP Address. IPAddress.Broadcast = Used to send. I want to send a packet to anyone who is listening.

IPAddress.Any = 用于接收。我想监听到达任何 IP 地址的数据包。IPAddress.Broadcast = 用于发送。我想向正在收听的任何人发送一个数据包。

回答by Adam Alexander

for your purposes I believe you will want to use IPAddress.Any instead of IPAddress.Broadcast. Hope this helps!

出于您的目的,我相信您会想要使用 IPAddress.Any 而不是 IPAddress.Broadcast。希望这可以帮助!

回答by leppie

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 范围。

回答by sipwiz

There's nothing wrong with the way you have configured your UdpClient. Have you tried a different port number? Perhaps 1234 is already in use on your system by a different app.

您配置 UdpClient 的方式没有任何问题。您是否尝试过不同的端口号?也许 1234 已被其他应用程序在您的系统上使用。