ios GCDAsyncSocket收不到服务器的数据,发送数据正常

qq_31813867 2016-11-17 02:58:04
-(void)startConnectSocket{
self.socket =[[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];

if (![self.socket isConnected]) {
NSError * error=nil;
if ( [self.socket connectToHost:HOST onPort:POST error:&error]) {
NSLog(@"%@",[error description]);
}
else{

NSLog(@"失败");
}
}

}


#pragma mark当链接成功时就会调用
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
NSLog(@"%@",host);
// self.HearTimer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(checkLongConnectByServe) userInfo:nil repeats:YES];

// [self checkLongConnectByServe];
// [sock readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:200];
// [self.HearTimer fire];

//等待服务器消息,
NSLog(@"%@",[NSThread currentThread]);
[sock readDataWithTimeout:-1 tag:200];
}

#pragma mark当服务器给客服端发送消息时调用
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

NSMutableDictionary * dicData= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"%@",dicData);
[sock readDataWithTimeout:-1 tag:200];
}
[size=16px] 第一次链接,服务器就会给客服端发送数据,可是接受不到,为什么??[/size]
...全文
668 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gangAndgang 2016-11-20
  • 打赏
  • 举报
回复
你用的事件方式在获取服务器发来的数据, 应该不需要使用 [sock readDataWithTimeout:-1 tag:200]; 来主动读,这个会一直block当前线程的
qq_31813867 2016-11-17
  • 打赏
  • 举报
回复
在线等 速速 大神求教

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧