IOS uses Socket UDP protocol Broadcast Mechanism
1. Preface
What is the UDP protocol broadcast mechanism?
For example, if a person in a group of people is looking for James, then you yell (broadcast): "Who is James"
If it is James, it will respond to you, and it is the same in the network.
Application scenarios of UDP broadcast mechanism:
Several clients need to find unique devices, such as servers, printers, and fax machines, in the LAN (without knowing the IP address) of many devices.
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-side iPhone: Client
. Server (iPad) Implementation
// Initialize udp
AsyncUdpSocket * asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate: self];
// Bind the 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.1. Implement proxy Methods
// 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 didNotReceiveDataWithTag :( long) tag dueToError :( NSError *) error {
}
// No message is sent
-(Void) onUdpSocket :( AsyncUdpSocket *) sock didNotSendDataWithTag :( long) tag dueToError :( NSError *) error {
}
// The message has been sent.
-(Void) onUdpSocket :( AsyncUdpSocket *) sock didSendDataWithTag :( long) tag {
}
// Disconnect
-(Void) onUdpSocketDidClose :( AsyncUdpSocket *) sock {
}
2. Implementation of the client (iPhone)
Note: The implementation steps are similar to those on the server.
// Initialize udp
AsyncUdpSocket * asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate: self];
// Bind the port
NSError * err = nil;
[AsyncUdpSocket enableBroadcast: YES error: & err];
[AsyncUdpSocket bindToPort: 9527 error: & err];
2.2.1. Implement proxy Methods
// Received message
-(BOOL) onUdpSocket :( AsyncUdpSocket *) sock didReceiveData :( NSData *) data withTag :( long) tag fromHost :( NSString *) host port :( UInt16) port {
Return YES;
}
// No message is 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 {
}
// The message has been sent.
-(Void) onUdpSocket :( AsyncUdpSocket *) sock didSendDataWithTag :( long) tag {
}
// Disconnect
-(Void) onUdpSocketDidClose :( AsyncUdpSocket *) sock {
}
2.2.2. 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: @ brief seconds 255.255 port: 9527 withTimeout:-1 tag: 0];