简体   繁体   English

“请求的地址在其上下文中无效” SocketException

[英]“The requested address is not valid in its context” SocketException

I'm trying to send a broadcast packet (dest: FF.FF.FF.FF) to the local network on port 45624, except that an exception is being thrown when the UdpClient is created. 我正在尝试在端口45624上向本地网络发送广播数据包(目标:FF.FF.FF.FF),但在创建UdpClient时会引发异常。 I've tried this with other addresses, including ones which aren't actually subnet masks, but it always raises that exception. 我已经尝试了其他地址,包括那些实际上不是子网掩码的地址,但它总是引发该异常。 Ditto for ports other than 45624. 与45624以外的其他端口相同。

byte[] SearchAddr = new byte[] { 255, 255, 255, 255 };
IPAddress SearchIP = new IPAddress(SearchAddr);
IPEndPoint EndPoint = new IPEndPoint(SearchIP, 45624);
UdpClient Searcher = new UdpClient(EndPoint);
byte[] SearchPacket = new byte[] { 0x34, 0x00, 0xB0, 0xFA, 0x02, 0x00, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x70, 0x6C, 0x61, 0x79, 0x02, 0x00, 0x0E, 0x00, 0x20, 0x74, 0x79, 0x99,
    0xF5, 0xF5, 0xCF, 0x11, 0x98, 0x27, 0x00, 0xA0, 0x24, 0x14, 0x96, 0xC8, 0x00, 0x00, 0x00, 0x00,
    0x81, 0x00, 0x00, 0x00 };
Searcher.Send(SearchPacket, SearchPacket.Length);
byte[] SearchReply = Searcher.Receive(ref EndPoint);
string ReplyText = "";
for (int I = 0; I < SearchReply.Length; I++)
    ReplyText += SearchReply[I].ToString("X2") + " ";
MessageBox.Show(ReplyText);

Does anyone know what, exactly, the problem is? 谁知道问题到底在哪里吗?

The documentation for UdpClient.Send sheds light on the issue: UdpClient.Send的文档UdpClient.Send了该问题:

This overload sends datagrams to the remote host established in the Connect method and returns the number of bytes sent. 此重载将数据报发送到在Connect方法中建立的远程主机,并返回发送的字节数。 If you do not call Connect before calling this overload, the Send method will throw a SocketException . 如果在调用此重载之前未调用Connect ,则Send方法将抛出SocketException

You should use this overload instead: 您应该改用以下重载

Searcher.Send(SearchPacket, SearchPacket.Length, EndPoint);

暂无
暂无

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

相关问题 SocketException:请求的地址在上下文中无效 - SocketException: The requested address is not valid in the context 面临错误 tcp 侦听器 SocketException:{0}System.Net.Sockets.SocketException (0x80004005):请求的地址在其上下文中无效 - Facing error tcp listener SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context 套接字错误请求的地址在其上下文中无效 - Socket Error The requested address is not valid in its context 当我尝试侦听端口时,请求的地址在其上下文中无效 - The requested address is not valid in its context when I try to listen a port 获取请求的地址在其上下文中无效。 在我的聊天应用程序中 - Getting The requested address is not valid in its context. in my chat application WCF Tcp绑定“请求的地址在其上下文中无效”错误 - WCF Tcp Binding “The requested address is not valid in its context” Error HttpListener:请求的地址在此上下文中无效 - HttpListener: The requested address is not valid in this context 为什么我的服务器 TcpListener 使用本地地址而不使用公共地址? - 例外:请求的地址在其上下文中无效 - Why is my Server TcpListener Working with Local Address but not with Public Address? - Exception: The requested address is not valid in its context 32feet .NET api拒绝连接“请求的地址在其上下文中无效” - 32feet .NET api refusing to connect “The requested address is not valid in its context” Socket.bind()在Win7上返回“请求的地址在其上下文中无效。” - Socket.bind() returns “The requested address is not valid in its context.” on Win7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM