首页 > 代码库 > xcode中的udp接收数据

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

 

}