1. PrefaceWhat is the UDP protocol broadcast mechanism? Take an example, for example, in a group of people, a person to find Zhang San, so you shout to the Crowd (broadcast): "Who is Zhang San" if it is Zhang San, it will respond to you, in the network is the same.application scenarios for UDP broadcast mechanism:A number of clients, in the LAN (do not know the case of IP) need to find a number of devices unique equipment, such as the server, or a printer, fax machines and so on. Let's say I'm now ready to install the server on an ipad that never loses power. A number of clients iphone is activated, it is necessary to broadcast to all devices, who is the server, the server, please tell me the IP address. Then I went to connect and then entered the long connection and received the message backstage.
implementation of 2.UDP broadcasting mechanismNote:iPad: Server-side iPhone: Client2.1. Server-side (IPAD) Implementation
2.1.1. Download the AsyncUdpSocket.h framework package on GitHub first
Www.github.com
2.1.2. Initializing UDP
?
| 1 2 3 4 |
@interface QCViewController (){ AsyncUdpSocket *asyncUdpSocket; } asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; |
2.1.3. Binding port
?
| 1 2 3 4 5 6 7 8 |
NSError *err = nil; [asyncUdpSocket enableBroadcast:YES error:&err]; [asyncUdpSocket bindToPort:9527 error:&err]; //启动接收线程 [asyncUdpSocket receiveWithTimeout:-1 tag:0]; |
2.1.4. Implementing Proxy methods
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//已接收到消息 - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ if(data是找服务器的){ //根据客户端给的IP,利用TCP或UDP 相互连接上就可以开始通讯了 } return YES; } //没有接受到消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{ } //没有发送出消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{ } //已发送出消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ } //断开连接 -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{ } |
2.2. The implementation of the client (IPhone)
Note: implementation steps similar to server-side
2.2.1. Initializing UDP
?
| 1 2 3 4 |
@interface QCViewController (){ AsyncUdpSocket *asyncUdpSocket; } asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; |
2.2.2. Binding port
?
| 1 2 3 4 |
NSError *err = nil; [asyncUdpSocket enableBroadcast:YES error:&err]; [asyncUdpSocket bindToPort:9527 error:&err]; |
2.2.3. Implementing Proxy methods
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//已接收到消息 - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ return YES;} //没有接受到消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{ } //没有发送出消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{ } //已发送出消息 -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ } //断开连接 -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{ } |
2.2.4. Radio Search
Note: The broadcast IP address is 255.255.255.255
?
| 1 2 3 4 5 6 7 8 9 |
NSString *str = @"谁是服务器?我的IP是:192.168.80.103";NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];[asyncUdpSocket sendData:data toHost:@"255.255.255.255 port:9527 withTimeout:-1 tag:0]; |
Clear Saup
Source: http://www.cnblogs.com/qingche/
This article is copyright to the author and the blog Park is shared, welcome reprint, but must retain this paragraph statement, and in the article page obvious location to give the original text connection.