Application Scenario: The client (mobile phone, PC) needs to search for servers in the local area network and obtain the server address.
Method Introduction: The client sends the UDP broadcast, the service receives the client IP address after receiving the broadcast, then sends a socket link to the client, the client receives the socket link, obtains the server address.
Related knowledge:
UPD, TCP. TCP is a link-oriented, reliable means of communication. UDP is a non-link-oriented method of communication. The establishment of TCP is more troublesome than the "three-time handshake". The establishment of UDP is relatively simple, the sender just send the content, regardless of whether the receiver received. The transmission of UDP is divided into: unicast, multicast, broadcast. Where multicast and broadcast are implemented through a specific IP address within the TCP/IP protocol, for example, the address of a restricted broadcast is: 255.255.255.255.
A total of 65,535 network ports, the communication between the host needs to develop a port. Network ports are divided into three categories, recognized ports, registered ports, and dynamic and/or private ports.
Experimental method:
Four applications, of which two Android programs, two PC programs. Let the PC and Android take turns, or together as both client and server. The test results are: The UDP listener on the PC and the UDP initiator can run concurrently. But on Android phones, once the UDP service is listening on the port, the native can no longer send UDP broadcasts for that port.
Code Download: http://download.csdn.net/detail/qiushibaiyi/7531837
Network Port Data entry: Http://baike.baidu.com/view/43200.htm?fr=aladdin Click to open the link
Multicast and broadcast data entry: http://www.cnblogs.com/hnrainll/archive/2011/09/01/2162110.html Click to open link
Section Code Reference: http://download.csdn.net/download/xiaoxiaobian3310903/4502201# Click to open the link
Reference article: http://blog.sina.com.cn/s/blog_6c0a8fbb0100s3k5.html Click to open link
Code address: http://download.csdn.net/detail/qiushibaiyi/7531837 Click to open the link