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

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:

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.

You should use this overload instead:

Searcher.Send(SearchPacket, SearchPacket.Length, EndPoint);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top