1. preface what is UDP protocol broadcast mechanism? For example, if a person in a group of people is looking for Michael Jacob, then you shout (broadcast) to the group: "Who is James?" If it is James, it will respond to you, and the same is true in the network. Application scenarios of UDP broadcast mechanism: several clients need to find unique devices, such as servers, in the LAN (without knowing the IP address) on many devices, or a printer or a fax machine. Suppose I want to install the server on an iPad that never loses power. When several clients activate the iPhone, they will broadcast to all devices. If they are servers and servers, please tell me the IP address. Then I connect to the server and enter the persistent connection to receive messages in the background. 2. UDP broadcast mechanism implementation Note: iPad: Server iPhone: Client 2. 1. server (iPad) Implementation 2.1.2. initialize udp @ interface QCViewController () {AsyncUdpSocket * asyncUdpSocket;} asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate: self]; 2.1.3. binding port NSError * err = nil; [asyncUdpSocket enableBroadcast: YES error: & err]; [asyncUdpSocket bindToPort: 9527 error: & err]; // start the receiving thread [asyncUdpSocket receiveWithTimeout: -1 tag: 0]; 2.1.4. implement proxy method // received message- (BOOL) onUdpSocket :( AsyncUdpSocket *) sock didReceiveData :( NSData *) data withTag :( long) tag fromHost :( NSString *) host port :( UInt16) port {if (data is for the server) {// Based on the IP address provided by the client, TCP or UDP can be connected to each other to Start Communication} return YES;} // No message is received-(void) onUdpSocket :( AsyncUdpSocket *) sock handle :( long) tag dueToError :( NSError *) error {}// no message is sent-(void) onUdpSocket :( AsyncUdpSocket *) sock didNotSendDataWithTag :( long) tag du EToError :( NSError *) error {} // a message has been sent-(void) onUdpSocket :( AsyncUdpSocket *) sock didSendDataWithTag :( long) tag {} // disconnect-(void) onUdpSocketDidClose :( AsyncUdpSocket *) sock {} 2. 2. client (iPhone) implementation Note: implementation steps are similar to those on the server side 2.2.1. initialize udp @ interface QCViewController () {AsyncUdpSocket * asyncUdpSocket;} asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate: self]; 2.2.2. binding port NSError * err = nil; [asyncUdpSocket enableBroadc Ast: YES error: & err]; [asyncUdpSocket bindToPort: 9527 error: & err]; 2.2.3. implement proxy method // received message-(BOOL) onUdpSocket :( AsyncUdpSocket *) sock didReceiveData :( NSData *) data withTag :( long) tag fromHost :( NSString *) host port :( UInt16) port {return YES;} // No message received-(void) onUdpSocket :( AsyncUdpSocket *) sock didNotReceiveDataWithTag :( long) tag dueToError :( NSError *) error {} // No message is sent-(void) onUdpSocket :( AsyncUdpSocket *) sock DidNotSendDataWithTag :( long) tag dueToError :( NSError *) error {}// a message has been sent-(void) onUdpSocket :( AsyncUdpSocket *) sock didSendDataWithTag :( long) tag {} // disconnect-(void) onUdpSocketDidClose :( AsyncUdpSocket *) sock {} 2.2.4. broadcast search note: the broadcast IP address is 255.255.255.255 NSString * str = @ "who is the server? My IP address is: 192.168.80.103 "; NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; [asyncUdpSocket sendData: data toHost: @" commandid 255.255 port: 9527 withTimeout:-1 tag: 0];