0

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. enter image description here

Stella
  • 1,728
  • 5
  • 41
  • 95
  • Can you post your client code that gets data from image and sends it? Also UDP might be a bit tricky since it does not guarantee the order of the received packages (since data will be probably split into several packages). TCP requires a connection, true, but it is far more reliable. UDP is great for short strings that present no big problem if a few of them is lost... – Rok Jarc Jan 08 '14 at 14:36
  • Just for a test try to send a small png (20x20 pixels) instead of whole screenshot - to see if problem is in the size. – Rok Jarc Jan 08 '14 at 14:45
  • Show us how you create the socket object. – vikingosegundo Jan 08 '14 at 14:52
  • I Tried to send a 2 KB size .jpg file data using the same code, but its not doing(sending or receiving) anything. – Stella Jan 08 '14 at 14:53
  • They have given nice sample for desktop server connection, desktop UI and iOS client to send data as packets as well. I can go with this samples, if i could achieve sending image data as well. – Stella Jan 08 '14 at 14:54
  • Hi vikingosegundo, Its there in their project only, I am not doing anyting further, I am just testing their code now and trying to send image data instead of string data. There is no issue when I'm sending some string data. The project is here, https://github.com/robbiehanson/CocoaAsyncSocket – Stella Jan 08 '14 at 14:56
  • How are you checking on the other side that text comes trough and image doesn't? And (just to be sure) try to make a PNG 500b or less. – Rok Jarc Jan 08 '14 at 14:58
  • CocoaAsyncSocket has a server project(OS X), which establishes the socket connection with whichever client connects, and they have a desktop UI, where we can see the received data (string) coming from iOS client app. Here it is, 'UdpEchoServer/Desktop' – Stella Jan 08 '14 at 15:00
  • why aren't you using some solid http connection? – vikingosegundo Jan 08 '14 at 15:03
  • My requirement is not there using http connection. I have to use socket code only, this will be implemented for different purposes. – Stella Jan 08 '14 at 15:05
  • @User817729: i know you stated that "socket communication" is a requirement for you. But vikingosegundo has a point: HTTP could be considered as a socket communication. – Rok Jarc Jan 08 '14 at 15:05
  • From this query, http://stackoverflow.com/questions/20945892/ios-socket-communication-for-sending-image-data I got information from you to use CocoaAsyncSocket for same purpose, and trying now, I don't know what to do on this? Do CocoaAsyncSocket given any sample for http for using socket connection sending packets? – Stella Jan 08 '14 at 15:07
  • I tried sending 140 bytes .png image data via the same channel code from iOS client, but its not sending. – Stella Jan 08 '14 at 15:09
  • In that case maybe the problem is simply in the way you are checking if the data came trough. Can you post also that server method? – Rok Jarc Jan 08 '14 at 15:11
  • Well..I retested again, I'm able to send this small image successfully, as rokjarc suggested. – Stella Jan 08 '14 at 15:13
  • 1
    Now you should use tcp instead of udp – vikingosegundo Jan 08 '14 at 15:14
  • OK..Please share me samples to learn.. – Stella Jan 08 '14 at 15:15
  • CocoaAsyncSocket also has TCP functionality. – Rok Jarc Jan 08 '14 at 15:19
  • Oh, that's great! Could you please help me which is their project source in their bundle doing it for this? – Stella Jan 08 '14 at 15:22
  • SimpleHTTPClient and EchoServer should get you on the right path... – Rok Jarc Jan 08 '14 at 15:27
  • Thank you very much! Does EchoServer create a server for us? What is that server? I'm learning about this still. – Stella Jan 09 '14 at 05:28
  • Using SimpleHTTPClient and EchoServer, I am facing two issues, 1.) iOS Client is getting disconnected with the Echo server in every 2 or 3 mins..not sure why. 2.) When i transfer a big size image, on the server front end view, its loading and then immediately throwing error as 'Error converting received data into UTF-8 String'. – Stella Jan 09 '14 at 05:33
  • please check OP for updated query. – Stella Jan 09 '14 at 07:57
  • Hi rokjarc, Could you please help sorting this issue? – Stella Jan 09 '14 at 08:06
  • Did you ever figured this out? I'm having the exact same issue ... – Herz Rod Feb 14 '14 at 16:11
  • no, i couldn't..looking into different stuff..if you find some fix, please update here.. – Stella Feb 15 '14 at 14:19
  • I actually got it working, the problem was the way I was turning the UIImage into NSData and from NSData to the UIImage ... If you get it with UIImageJPEGRepresentation() you should write it with UIImageWriteToSavedPhotosAlbum() because the way the UIImage is turned into data is different than any other reading methods (like [UIImage imageNamed] or +imageWithContentsOfFile ... Try it and let me know the results – Herz Rod Feb 21 '14 at 05:06

0 Answers0