首页 > iOS UDP socket 通讯 使用GCDAsyncUdpSocket 服务器无法接受到消息?求大神解决

iOS UDP socket 通讯 使用GCDAsyncUdpSocket 服务器无法接受到消息?求大神解决

现在我要使用udp socket进行聊天通讯,代码如下

**_udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    NSError * error = nil;
    [_udpSocket bindToPort:udpPort error:&error];
    [_udpSocket enableBroadcast:YES error:&error];
    if (error) {
        NSLog(@"error:%@",error);
    }else {
        [_udpSocket beginReceiving:&error];
    }
 [self sendMessage:textField.text andType:@0];
    
    NSString * ipAddress = [self deviceIPAdress];
    
    MessageInfo *info = [[MessageInfo alloc]init];
    info.sendId = @"201312260000014064";
    info.targetId = @"201312260000014063";
    info.info = @"你好";
    info.device = @"iOS";
    NSData * sendData = [self returnDataWithObject:info];
    
    [_udpSocket sendData:sendData toHost:@"10.10.2.73" port:udpPort withTimeout:-1 tag:0];
    
    textField.text = nil;
    
    return YES;

通讯具体过程是我把要发送的数据封装成类 然后通过流的形式发送到服务器,GCDAsyncUdpSocket应该会自己处理流(不确定),现在服务器收不到消息,服务器Host是10.10.2.73 端口是8888


你先试试用这个方法连接一下服务器咯

- (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port error:(NSError **)errPtr;

然后你试试在代理中实现这个

/**
 * Called when the datagram with the given tag has been sent.
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag;

看看数据到底有没有发出去。


感觉没哪里有错,但就是收不到数据这是什么原因

【热门文章】
【热门文章】