我正在研究设备查找器,我真的很喜欢代码,因为通常我会编写微控制器代码。
我希望有人可以帮助我。
目标是从UDP广播接收客户端IP到制造商特定端口。 这对我来说很好。我发送了广播,然后我收到了从设备发送的数据:
00-00-00-F7-00-20-A0-06-58-39-30-12-63-16-00-00-62-A7-52-0B-FF-00-00-00 -00-80-A3-BE-2F-XX
它包括设备的MAC地址(最后6个HEX片段)
但我找不到发件人(客户端)的IP地址
如何从客户端保存整个打包发送?包括IP? 因为在Wireshark中我看到整个包(发送自:192.xxx ......)
非常感谢你! 最好的问候
我的代码是:
Byte [] data = {0x00,0x00,0x00,0xF6};
//string s1 = Encoding.UTF8.GetString(data);
int port = 30718;
string Antwort;
// Socket definieren
Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//bcSocket.Connect(new IPEndPoint(IPAddress.Broadcast, port));
// EndPoint definieren bzw. Ziel des Broadcastes
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);
// Optionen auf den Socket binden
bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
// Broadcast senden
bcSocket.SendTo(data, iep);
bcSocket.ReceiveTimeout = 5000;
byte[] test = new byte[1024];
bcSocket.Receive(test);
Antwort = System.Text.Encoding.Default.GetString(test).Trim(new char[] { '\0' });
textBox_IPAdresse.Text = Antwort;
string antworthex = BitConverter.ToString(test);
textBox1.Text = antworthex;
// Socket schliessen, nach erfolgreichem Senden des Broadcastes
bcSocket.Close();
答案 0 :(得分:1)
100%正常工作!你救了我的一天。谢谢!
此处为搜索的代码:
(我用它在我的网络中找到Lantronix Xport设备并获得他的IP)
Byte [] data = {0x00,0x00,0x00,0xF6};
//string s1 = Encoding.UTF8.GetString(data);
int port = 30718;
string Antwort;
// Socket definieren
Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//bcSocket.Connect(new IPEndPoint(IPAddress.Broadcast, port));
// EndPoint definieren bzw. Ziel des Broadcastes
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);
// Optionen auf den Socket binden
bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
// Broadcast senden
bcSocket.SendTo(data, iep);
bcSocket.ReceiveTimeout = 5000;
byte[] test = new byte[1024];
IPEndPoint _sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)_sender;
bcSocket.ReceiveFrom(test, ref senderRemote); //IP steht in senderRemote! / IP is in senderRemote!
Antwort = System.Text.Encoding.Default.GetString(test).Trim(new char[] { '\0' });
textBox_IPAdresse.Text = senderRemote.ToString(); //Ausgabe der RemoteIP
string antworthex = BitConverter.ToString(test);
textBox1.Text = antworthex;
// Socket schliessen, nach erfolgreichem Senden des Broadcastes
bcSocket.Close();
答案 1 :(得分:0)
您必须使用PEN
byte[] test = new byte[1024];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)sender;
bcSocket.Receive(test, ref senderRemote);
现在,sender
变量中提供了远程IP。