GCDAsyncSocket的问题

Scholar0 Scholar0 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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分送上
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
Scholar0
iOS 29,038 成员 12,459 内容