我正在尝试使用C#查找网络上的Lantronix XPort Pro设备。我使用在Lantronix开发者wiki上找到的一些Python代码作为示例http://wiki.lantronix.com/developer/Lantronix_Discovery_Protocol。
我正在编写的应用程序是用C#编写的,我需要发现已安装Lantronix设备的单元。当我执行socket.RecieveFrom函数调用时,似乎它只是使应用程序停顿了。
你有什么想法我做错了什么?来自上面链接的python代码正确地检测到设备。我应该能够在C#中复制它。
非常感谢您的任何帮助。
我正在编写的应用程序是用C#编写的,我需要发现已安装Lantronix设备的单元。当我执行socket.RecieveFrom函数调用时,似乎它只是使应用程序停顿了。
你有什么想法我做错了什么?来自上面链接的python代码正确地检测到设备。我应该能够在C#中复制它。
非常感谢您的任何帮助。
private void FindLantronixXPort()
{
// This is the socket code that will broadcast from
// the local machine looking for responces from Lantronix
// XPort servers
// Create the array for our message chars
char[] chars = new char[4];
// Build the actual message
chars[0] = Convert.ToChar(0);
chars[1] = Convert.ToChar(0);
chars[2] = Convert.ToChar(0);
chars[3] = Convert.ToChar(0xf6);
// Convert the chars to a message string
string msg = new string(chars);
// Convert the setring to a byte array
byte[] data = Encoding.UTF8.GetBytes(msg);
// Get the local machines IP address
string Local_IP = GetIPAddress();
// Now create a broadcast UDP socket
Socket XmtSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
XmtSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(Local_IP), LantronixPort);
// Broadcast the packet
XmtSock.SendTo(data, 0, data.Length, SocketFlags.None, iep);
XmtSock.Close();
// Wait 500 mili seconds
int milliseconds = 500;
System.Threading.Thread.Sleep(milliseconds);
Socket RcvSock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
iep = new IPEndPoint(IPAddress.Any, LantronixPort);
RcvSock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive...");
byte[] data1 = new byte[120];
int recv = RcvSock.ReceiveFrom(data1, data1.Length, SocketFlags.None, ref ep);
string stringData = Encoding.ASCII.GetString(data1, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
RcvSock.Close();
}