你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios開發之發送UDP廣播並接收數據

ios開發之發送UDP廣播並接收數據

編輯:IOS開發綜合
用了asyncUdpSocket, 服務端在接收到廣播後,就會做出回應,然後就可以開始通訊了。

- (void)init{

AsyncUdpSocket *socket=[[AsyncUdpSocketalloc]initWithDelegate:self];

[socketlocalPort:16747];

NSTimeInterval timeout=5000;

NSString *request=@quick_stat;

NSData *data=[NSDatadataWithData:[request dataUsingEncoding:NSASCIIStringEncoding] ];

UInt16 port=8081;

NSError *error;

[socket enableBroadcast:YESerror:&error];

 

[socket sendData :data toHost:@255.255.255.255 port:port withTimeout:timeout tag:1];

[socket receiveWithTimeout:-1tag:0];

NSLog(@begin scan);

}

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{

NSString* result;

result = [[NSStringalloc] initWithData:data encoding:NSASCIIStringEncoding];

NSLog(@%@,result);

NSLog(@received);

}

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{

NSLog(@not received);

}

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{

NSLog(@%@,error);

NSLog(@not send);

}

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{

NSLog(@send);

}

-(BOOL)onUdpSocketDidClose:(AsyncUdpSocket *)sock{

NSLog(@closed);

}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved