UdpClient 在广播地址上接收

发布于 2024-07-16 20:55:35 字数 522 浏览 6 评论 0原文

在 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);
        ...

但是我收到一个套接字异常,指出该地址不可用,错误代码为 10049 我该怎么做才能否定这个例外?

In c# I am using the UdpClient.Receive function:

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?

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

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

发布评论

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

评论(4

指尖凝香 2024-07-23 20:55:35

这是我目前在一个有效的生产应用程序中使用的一些代码的要点(我们有一些额外的代码来处理客户端是服务器应用程序在独立安装上运行的情况)。 它的工作是接收消息已准备好处理的 udp 通知。 正如 Adam Alexander 所提到的,您唯一的问题是您需要使用 IPAddress.Any,而不是 IPAddress.Broadcast。 仅当您想要发送广播 UDP 数据包时,才会使用 IPAddress.Broadcast。

设置 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.

并使用回调触发异步接收的开始。

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

希望这会有所帮助,您应该能够使其适应同步工作而不会出现太多问题。 与你正在做的事情非常相似。 如果此后您仍然收到错误,则一定有其他东西正在使用您尝试侦听的端口。

所以,澄清一下。

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

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 Send a broadcast UDP packet.

Set up the udp client

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.

心不设防 2024-07-23 20:55:35

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

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

伴梦长久 2024-07-23 20:55:35

该错误意味着协议无法绑定到所选的 IP/端口组合。

我已经很久没有使用 UDP 广播了,但我记得你需要使用不同的 IP 范围。

That error means the protocol cant bind to the selected IP/port combination.

I havent used UDP broadcast in ages, but I do recall you need to use different IP ranges.

人心善变 2024-07-23 20:55:35

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

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.

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