First, send a broadcast to the network through UDP:
Code
Class Program {
Static void Main (string [] args ){
UdpClient client = new UdpClient ();
While (true ){
String s = "FindTerminals ";
Byte [] bytes = Encoding. ASCII. GetBytes (s );
Try {
Client. Connect (IPAddress. Broadcast, 3600 );
Client. Send (bytes, bytes. Length );
Console. WriteLine (Encoding. ASCII. GetString (bytes ));
} Catch (Exception ex ){
Console. WriteLine (ex );
Client. Close ();
Break;
}
Thread. Sleep (1000 );
}
Console. WriteLine ("= End = ");
Console. ReadKey ();
}
}
Run a UDP listening process on the device to report the network broadcast of the server:
Code
Class Program {
Static void Main (string [] args ){
UdpClient client = new UdpClient (3600 );
While (true ){
IPEndPoint RemoteIpEndPoint = new IPEndPoint (IPAddress. Any, 0 );
Try {
Byte [] buffer = client. Receive (ref RemoteIpEndPoint );
String data = Encoding. ASCII. GetString (buffer );
Console. WriteLine ("Message: {0} from {1}", data. ToString (), RemoteIpEndPoint. Address. ToString ());
// After obtaining the Server IP address, you can establish a network connection and report the local information to the server.
} Catch (Exception ex ){
Console. WriteLine (ex );
Client. Close ();
Break;
}
}
Console. WriteLine ("= End = ");
Console. ReadKey ();
}
}
After the device obtains the Server IP address, it can establish a network connection to report the local information to the server. After receiving the feedback from the device, the server extracts the device information, such as the IP address and MAC address, to complete the networking.