Networking with devices that use DHCP to obtain IP addresses

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.