问题
I am using GCD code of CocoaAsyncSocket-master source. I am using 'EchoServer' for running server version application and 'SimpleHTTPClient' client application for running on my iOS device. With the sample they given, I can run the desktop server app and iOS client app and get it connected without any issues.
I am trying now to send an UIImage data to the server app, instead of http header they are sending in the existing client project. So, I am sending UIImage data as per the below code on the client side. But, the issue is, it is NOT sending this image data properly to the server front end app.
Code: https://github.com/robbiehanson/CocoaAsyncSocket
I use the same project 'CocoaAsyncSocket-master/GCD/Xcode/SimpleHTTPClient' provided by CocoaAsyncSocket and just changed the below code for sending a sample image.
SimpleHTTPClient client code for sending :
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
DDLogVerbose(@"socket:didConnectToHost:%@ port:%hu", host, port);
//NSString *requestStrFrmt = @"HEAD / HTTP/1.0\r\nHost: %@\r\n\r\n";
//NSString *requestStr = [NSString stringWithFormat:requestStrFrmt, HOST];
//NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];
UIImage *img = [UIImage imageNamed:@"2.jpg"];
//NSData *data = UIImagePNGRepresentation(img);
NSData *data = UIImageJPEGRepresentation(img, 1.0);
[asyncSocket writeData:data withTimeout:-1.0 tag:0];
}
Server code at receiving...
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
// This method is executed on the socketQueue (not the main thread)
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
//NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
//NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
NSImage *sharedImage = [[NSImage alloc] initWithData:data];
//if (sharedImage)
//if (msg)
{
//[self logMessage:msg];
[imageView setImage:sharedImage];
}
/*else
{
[self logError:@"Error converting received data into UTF-8 String"];
}*/
}
});
// Echo message back to client
[sock writeData:data withTimeout:-1 tag:ECHO_MSG];
}
Issues:
Using SimpleHTTPClient and EchoServer, I am facing the following two issues, 1.) iOS Client is getting disconnected with the Echo server after 2 or 3 mins... not sure why, may be there is some timeout. 2.) When i transfer a big size image, its receiving and loading some bit of image and then immediately throwing error as 'Error converting received data into UTF-8 String' on the server front end log view, pls find the reference image.
来源:https://stackoverflow.com/questions/20997480/ios-cocoaasyncsocket-image-data-not-sending-from-client-cocoaasyncsocket