[英]“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 callConnect
before calling this overload, theSend
method will throw aSocketException
. 如果在调用此重载之前未调用Connect
,则Send
方法将抛出SocketException
。
You should use this overload instead: 您应该改用以下重载 :
Searcher.Send(SearchPacket, SearchPacket.Length, EndPoint);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.