iOS-CocoaAsyncSocket: Image data not sending from client CocoaAsyncSocket

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 04:28:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!