IOS uses Socket UDP protocol Broadcast Mechanism

Source: Internet
Author: User

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];



 

Related Article

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.