1.udpclient的初始化
udpClient=[[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
udpClient.delegate=self;
NSError *err;
BOOL bret=[udpClient bindToPort:8009 error:&err]; //绑定接收数据用的端口
BOOL ret=[udpClient beginReceiving:&err ]; //开启接收线程
2.定义接收事件
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
//储存文件
NSString *str=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(str);
}
原文地址:http://www.cnblogs.com/HuiLove/p/3910417.html