gpt4 book ai didi

c# - "The requested address is not valid in its context"套接字异常

转载 作者:行者123 更新时间:2023-11-30 20:50:48 26 4
gpt4 key购买 nike

我正在尝试将广播数据包(目标:FF.FF.FF.FF)发送到端口 45624 上的本地网络,但在创建 UdpClient 时抛出异常。我已经用其他地址尝试过这个,包括那些实际上不是子网掩码的地址,但它总是会引发异常。 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);

有谁知道问题到底是什么?

最佳答案

UdpClient.Send 的文档阐明了这个问题:

This overload sends datagrams to the remote host established in the Connect method and returns the number of bytes sent. If you do not call Connect before calling this overload, the Send method will throw a SocketException.

你应该使用 this overload相反:

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

关于c# - "The requested address is not valid in its context"套接字异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com