GCDAsyncSocket的问题

Scholar0 2015-06-24 05:30:35
iPhone是客户端,接收到服务器指令后 在
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

里面处理数据然后再返回给服务器。
但是app会100%崩溃,很奇怪。

代码如下:


#import "CommandClient.h"
#import <UIKit/UIKit.h>

@implementation CommandClient
+ (CommandClient *)sharedInstance {
static CommandClient *sharedInstance = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)initialize {

static dispatch_once_t once;
dispatch_once(&once, ^{
NSLog(@"Start Creating GCDAsyncSocket");
FMUtils = objc_getClass("FMUtils");
socketClient =[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
});
}
- (BOOL)startServer {
NSLog(@"Start startServer GCDAsyncSocket");

NSError *error;
NSString *host = @"192.168.0.9";
uint16_t port = 8666;
BOOL ret = [socketClient connectToHost:host onPort:port error:&error];
if (!ret) {
NSLog(@"socketClient Connection: %@", error);
}

return ret;
}

- (void)stopServer {
NSLog(@"Start stopServer GCDAsyncSocket");
[socketClient disconnect];


}


#pragma mark - GCDAsyncSocketDelegate
// 已连接
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
[socketClient readDataWithTimeout:-1 tag:0];

NSLog(@"Connected: %@:%d", host, port);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"State" message:@"Connection Server Success!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert addButtonWithTitle:@"Yes"];
[alert show];

}
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString* sName = [dict objectForKey:@"sName"];
if ([sName isEqual:@"getSkey"]) {
NSString* sid = [dict objectForKey:@"sid"];
NSString* result = [FMUtils encrySkey:sid];
NSData *requestData = [result dataUsingEncoding:NSUTF8StringEncoding];


[sock writeData:requestData withTimeout:-1. tag:0];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"State" message:@"send Server Success!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; //这两句注释掉就会百分百崩溃
[alert release]; //这两句注释掉就会百分百崩溃

}

[socketClient readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{

}
@end


很奇怪 不添加这两句就会崩溃

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"State" message:@"send Server Success!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert release];


错误信息:
引用
task_set_exception_ports(B07, 400, F03, 0, 0) failed with error (4: (os/kern) invalid argument)


而且我测试了下,就算添加上UIAlertView这两句代码,服务器不停的发包过来的情况下,只要滑动界面就会崩溃

请问大家这是什么问题,100分送上
...全文
710 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_29470295 2017-05-16
  • 打赏
  • 举报
回复
你会到主线程 在操作ui试试
kebing1011 2015-07-01
  • 打赏
  • 举报
回复
你使用方法有错误,didReadData会经常调用啊,数据不是一下子就全部过来的,一次读写是有buffer大小的。 就是说,每次didReadData收到的data不一定是完整的一次对方的write过来的。 比如对方写入2048 kb的数据,每次didReadData 可能才收到 1024的大小,你要收集至完整的一次包。 一般Demo里只是发送一个简单字符串,当然一次就能过来了。 希望你有所帮助。
chuchuzinnia 2015-06-26
  • 打赏
  • 举报
回复
应该是内存不够了。闪退了。用上多线程。 跟UIAlertview没有关系

29,027

社区成员

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

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