0

Im using this code to send image data (nsdata)to server

- (void)sendImageData: (UIImage *)image {
NSData *imageData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
[outputStream write:[imageData bytes] maxLength:[imageData length]];

}

problem is: when I select a small size image, server will receive completely image's data Ive sent. However, when I send a lager image, server will not receive all my data. Why? Help me? Server receive data code:

int n = read(newSocket, buffer, 1024*256);
if (n < 0) {
    NSLog(@"error reading from socket!");
    return;
}
NSString *buffer2string = [NSString stringWithFormat:@"%s",buffer];
[stringData appendString:buffer2string];
Matsu
  • 55
  • 5

1 Answers1

0

I assume you are using TCP and in a block mode. TCP won't send all the data in one time, so you should call read method in a while loop. (read method returns when there are 1 byte in tcp's receive buffer by default) You must close the client connection to tell the server you have finished transmitting, or the server will never leave the while loop.

I didn't test the code, you can try it and tell me if it doesn't work.

int position = 0 ;
while (1) {
    int n = read(newSocket, buffer+position, 1024*256-position);
    if (n < 0) {
        NSLog(@"error reading from socket!");
        break ;
    } else if (n == 0) {
        NSLog(@"socket closed by peer") ;
        break ;
    } else {
        position += n ;
    }
}
KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • Hi KudoCC, Thanks for you replying! I've tested your code: I send my imageDataLeng to the server before by: int32_t dataLength = [imageData length]; [outputStream write: (uint8_t *)&dataLength maxLength:4]; then I receive: dataleng to know lengOfDataReceive then while loop (stringData.length < lengOfDataReceive){//Do like you}. But lengOfData is much more than leng of DataReceive. I dont know why? – Matsu Aug 29 '14 at 09:16
  • @user3652608 what is `stringData.length` and `lengOfData` ? Can you show your server side code after modified. Please edit in your question, it's hard for me to look the code in comment. – KudoCC Aug 29 '14 at 09:20
  • stringData is MutableString like I post in myquestion, lengOfdataReceive = dataLength which sent from client before for server to know length of data client will send to – Matsu Aug 29 '14 at 09:25
  • Can I have your facebook id or skype id? My facebookid: facebook.com/tunghd.uet and my skype: tunghd.uet, please add me! I want to ask you more, I'm new in sockets and iOS :) – Matsu Aug 29 '14 at 09:26
  • @user3652608 Oh no, you can't try to explain a image data to a string. `position` is the length of data you receive. Sorry, this is a Chinese and facebook is forbidden, crazy country. – KudoCC Aug 29 '14 at 09:29
  • I hope you help me more, I really need your help! – Matsu Aug 29 '14 at 09:31
  • @user3652608 `position` in my example is the length of data you receive. You can read the book `unix network programming` – KudoCC Aug 29 '14 at 09:33
  • so when I know I've received all data from client? – Matsu Aug 29 '14 at 09:35
  • @user3652608 Good question, you can send a int value (4 byte) which indicate the length of image data before sending the image data. After server receive first 4 byte, it could know the length of image data. Another way is that you can close the stream after `[outputStream write:[imageData bytes] maxLength:[imageData length]];`. Tcp will continue to transmit the data to the server by default. On the server side, when `read` method returns zero, it means peer close the connection, and transmission is over. – KudoCC Aug 29 '14 at 09:40
  • I did like your comment: _italic_ **bold** - (void)sendImageData: (UIImage *)image { NSData *imageData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength]; [outputStream write:[imageData bytes] maxLength:[imageData length]]; [outputStream close]; } – Matsu Aug 29 '14 at 10:04
  • and the server: and server: while (1) { int n = read(newSocket, buffer+position, 1024*256-position); if (n < 0) { NSLog(@"error reading from socket!"); break ; } else if (n == 0) { NSLog(@"socket closed by peer") ; break ; } else { position += n ; NSString *buffer2string = [NSString stringWithFormat:@"%s", buffer]; [stringData appendString:buffer2string]; NSLog(@"stringData: %@", stringData); } } – Matsu Aug 29 '14 at 10:04
  • but this not quit while loop, why? @KudoCC – Matsu Aug 29 '14 at 10:05
  • sory for my comment, I dont know how to display code in commnet box – Matsu Aug 29 '14 at 10:07
  • @user3652608 What is the length of `imageData` ? Is it larger than 1024*256 ? – KudoCC Aug 31 '14 at 12:09
  • @user3652608 This [link](http://stackoverflow.com/questions/15526977/sending-uiimage-over-nsoutputstream) may help. You should also tweak your sending code. – KudoCC Sep 01 '14 at 01:38