xcode中的udp接收数据

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

 

}

 

posted @ 2014-08-13 16:11  LAOS  阅读(486)  评论(0编辑  收藏  举报