佚名通过本文主要向大家介绍了iOSUDPsocket通讯使用GCDAsyncUdpSocket服务器无法接受到消息?求大神解决等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: iOS UDP socket 通讯 使用GCDAsyncUdpSocket 服务器无法接受到消息?求大神解决
描述:
解决方案1:
描述:
现在我要使用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
解决方案1:
感觉没哪里有错,但就是收不到数据这是什么原因
解决方案2:你先试试用这个方法连接一下服务器咯
- (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;
看看数据到底有没有发出去。