I am having a socket listener program running(eclipse) on a mac machine and iOS client app is sending image to it in Bytes format. Normally, Image bytes will be 40 K and above.
I am facing a strange issue while reading the image bytes in socket. I have checked many links, they are suggesting to use like below code for reading all the bytes. The issue is, its reading all the bytes and NOT coming out of 'While' loop. After reading all the bytes, just struck inside the while loop only. I don't know what to do? Could someone please help me to solve this issue?

```
InputStream input = socket.getInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();    
byte[] bufferr = new byte[1024];
int read = 0;
long numWritten = 0;
try { 
    // Tried both the below while conditions, both are giving same issue     
    // while ((read = input.read(bufferr, 0, bufferr.length)) != -1) 

    while ((read = input.read(bufferr)) > 0) {
        baos.write(bufferr, 0, read);

        numWritten += read;
        System.out.println("numWritten: " + numWritten);      
    }
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}  
try {
    baos.flush();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}   
byte[] data = baos.toByteArray();

```

The below is my iOS code. I am closing the stream, still the same issue.

```
    -(void) shareImage
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    UIGraphicsBeginImageContext(appDelegate.window.bounds.size);
    [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    //[data writeToFile:@"screenshot.png" atomically:YES];

    NSLog(@"[data length] %i: ", [data length]);
    self.sentPing = YES;

    int num = [self.outputStream write:[data bytes] maxLength:([data length])];
    if (-1 == num) {
        NSLog(@"Error writing to stream %@: %@", self.outputStream, [self.outputStream streamError]);
    }else{
        NSLog(@"Wrote %i bytes to stream %@.", num, self.outputStream);
        [self.outputStream close];

        //NSTimer *myRegularTime = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(ShareNextScreen:) userInfo:nil repeats:NO];
    }

}

```