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 - 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];
错误信息:而且我测试了下,就算添加上UIAlertView这两句代码,服务器不停的发包过来的情况下,只要滑动界面就会崩溃请问大家这是什么问题,100分送上
-(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 - 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];
错误信息:而且我测试了下,就算添加上UIAlertView这两句代码,服务器不停的发包过来的情况下,只要滑动界面就会崩溃请问大家这是什么问题,100分送上
跟UIAlertview没有关系
就是说,每次didReadData收到的data不一定是完整的一次对方的write过来的。
比如对方写入2048 kb的数据,每次didReadData 可能才收到 1024的大小,你要收集至完整的一次包。
一般Demo里只是发送一个简单字符串,当然一次就能过来了。
希望你有所帮助。