In iPhone programming, how can I get an image file from TCP / IP from a server that has XP os?

I am making an image receiver from an iPhone.

The server sends an image file with a byte.

and I tried to make these bytes for the image.

But I can not make an image. already a few days ago ...

I really need your help ...

this is the source code i use.

Byte recvBuffer[500]; memset(recvBuffer, '\0', sizeof(recvBuffer)); [iStream read:recvBuffer maxLength:sizeof(recvBuffer)-1]; NSUInteger len = sizeof(recvBuffer); NSData *webdata = [NSData dataWithBytes:recvBuffer length:len]; imageview.image = [UIImage imageWithData:webdata]; 

if you give a comment, i will really be appriciate! thank you for reading!

+4
source share
2 answers

The badgerr answer is essentially the one I would use, but since the question is marked with Objective-C , the code really should be in Objective-C - something like the following. Note that through the TCP / IP connection, -read: maxLength: may return before the requested number of bytes has been read.

 uint32_t size; // Let make sure size is an explicit width. NSInteger totalBytesRead = 0; NSInteger bytesRead = [istream read: &size maxLength: sizeof size]; while (bytesRead > 0 && totalBytesRead + bytesRead < sizeof size) { totalBytes+= bytesRead; bytesRead = [istream read: &size + totalBytesRead maxLength: (sizeof size) - totalBytesRead]; } if (bytesRead >= 0) { totalBytesRead += bytesRead; } else { // read failure, report error and bail } if (totalBytesRead < sizeof size) { // connection closed before we got the whole size, report and bail } size = ntohl(size); // assume wire protocol uses network byte ordering NSMutableData* buffer = [[NSMutableData alloc] initWithLength: size]; totalBytesRead = 0; bytesRead = [istream read: [buffer mutableBytes] maxLength: size]; while (bytesRead > 0 && totalBytesRead + bytesRead < size) { totalBytes+= bytesRead; bytesRead = [istream read: (char*)[buffer mutableBytes] + totalBytesRead maxLength: size - totalBytesRead]; } if (bytesRead >= 0) { totalBytesRead += bytesRead; } else { // read failure, report error and bail (not forgetting to release buffer) } if (totalBytesRead < size) { // connection closed before we got the whole image, report and bail (not forgetting to release buffer) } else { [buffer setLength: size]; } imageView.image = [UIImage imageWithData: buffer]; [buffer release]; 

I typed the above directly into SO, so it was not tested or even compiled.

+3
source

TCP / IP is an OS independent protocol, so the fact that you are using XP on a server should not affect anything other than the potential use of the window API for socket operations.

Klaus Broch is right in saying that 499 bytes looks a bit small for the image. Instead, I suggest that you first find the total image size (in bytes) on the server, and then send this value at the beginning of the TCP stream, and then the image data. On the receiving side, your code must first read this size value, allocate some memory of that size, and then read that there are many bytes in it. You have not posted your server code, so I can not help it, but the client might look something like this:

  unsigned int size = 0; //assumes the size is sent as an int Byte* recvBuffer; [iStream read:&size maxLength:sizeof(unsigned int)]; recvBuffer = new Byte[size]; memset(recvBuffer, 0, size); [iStream read:recvBuffer maxLength:size]; NSData *webdata = [NSData dataWithBytes:recvBuffer length:size]; imageview.image = [UIImage imageWithData:webdata]; delete [] recvBuffer; 

Change My internal C ++ made me use the new and delete. Although you can use this in Obj-C using the suffix of the .mm file, this question is tagged with Objective-C. You should use JeremyP's answer as it also includes error checking and a more reliable way to read the full image.

+2
source

Source: https://habr.com/ru/post/1334474/


All Articles