UdpClient 在广播地址上接收
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我目前在一个有效的生产应用程序中使用的一些代码的要点(我们有一些额外的代码来处理客户端是服务器应用程序在独立安装上运行的情况)。 它的工作是接收消息已准备好处理的 udp 通知。 正如 Adam Alexander 所提到的,您唯一的问题是您需要使用 IPAddress.Any,而不是 IPAddress.Broadcast。 仅当您想要发送广播 UDP 数据包时,才会使用 IPAddress.Broadcast。
设置 udp 客户端
并使用回调触发异步接收的开始。
希望这会有所帮助,您应该能够使其适应同步工作而不会出现太多问题。 与你正在做的事情非常相似。 如果此后您仍然收到错误,则一定有其他东西正在使用您尝试侦听的端口。
所以,澄清一下。
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
And to trigger the start of an async receive using a callback.
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 而不是 IPAddress.Broadcast。 希望这可以帮助!
for your purposes I believe you will want to use IPAddress.Any instead of IPAddress.Broadcast. Hope this helps!
该错误意味着协议无法绑定到所选的 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.
您配置 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.