需求是iOS端發(fā)送udp消息指令喉脖,中控接收端接收做出相應(yīng)變化蓖救。用的GCDAsyncUdpSocket,直接pod導(dǎo)入或者github下載加入項(xiàng)目中
這里是客戶端酱虎,新建一個(gè)單例模式UDPManage類寨蹋,記得導(dǎo)入GCDAsyncUdpSocket和遵守協(xié)議
#define udpPort 6666
@interface UDPManage()<GCDAsyncUdpSocketDelegate>
@property (nonatomic, strong)GCDAsyncUdpSocket *udpSocket;
@end
static UDPManage *myUDPManage = nil;
@implementation UDPManage
+(instancetype)shareUDPManage{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
myUDPManage = [[UDPManage alloc]init];
[myUDPManage creatClientUdpSocket];
});
return myUDPManage;
}
-(void)creatClientUdpSocket{
self.udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
//綁定端口
NSError *error = nil;
[self.udpSocket bindToPort:udpPort error:&error];
//廣播
[self.udpSocket enableBroadcast:YES error:&error];
if (error) {
NSLog(@"error:%@",error);
}else{
[self.udpSocket beginReceiving:&error];
}
}
-(void)broadcast{
//消息內(nèi)容
NSString *str = @"這是一個(gè)測試廣播";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//如果向特定ip發(fā)送松蒜,這里要寫明ip
NSString *host = @"255.255.255.255";
[self.udpSocket sendData:data toHost:host port:udpPort withTimeout:-1 tag:100];
}
//成功
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
NSLog(@"發(fā)送成功嘞");
}
//失敗
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
NSLog(@"發(fā)送失敗嘞,錯(cuò)誤信息是:%@",error);
}
調(diào)用就直接
[[UDPManage shareUDPManage]broadcast];
服務(wù)端接收寫了個(gè)簡單的測試Demo
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t dQueue = dispatch_queue_create("", NULL);
self.receiveSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dQueue];
NSError *error;
//記得綁定端口已旧,和客戶端對應(yīng)的
[self.receiveSocket bindToPort:severPort error:&error];
if (error) {
NSLog(@"服務(wù)器綁定失敗");
}
[self.receiveSocket beginReceiving:nil];
}
//接收到消息后的協(xié)議方法
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext{
NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address];
uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"我收到了ip:%@秸苗,port:%hu,內(nèi)容是:%@ 的消息",ip,port,str);
//寫了個(gè)彈框展示收到的消息
UIAlertController* alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"ip:%@, port:%hu",ip,port]
message:[NSString stringWithFormat:@"內(nèi)容是:%@",str]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//響應(yīng)事件
NSLog(@"action = %@", action);
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//響應(yīng)事件
NSLog(@"action = %@", action);
}];
[alert addAction:defaultAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}