I have to send a PNG image through Cocoa Sockets (AsyncSockets) and I can’t manage to send it correctly, Firefox tells me the image is corrupted and Chrome doesn’t shows anything. I use this code:
content = [NSData dataWithContentsOfFile:filePath];
data = [[NSString stringWithFormat:@"HTTP/1.1 %@\nContent-Type: %@\n\n\n ", responseCode, contententType] dataUsingEncoding:NSUTF8StringEncoding];
[sock writeData:data withTimeout:-1 tag:1];
[sock writeData:[NSData dataWithBytes:[content bytes] length:[content length]] withTimeout:-1 tag:1];
As you can see I first send the headers and then I send the NSData bytes & length, but I can’t manage to get the PNG image correctly! What I’m doing wrong?
The line endings are wrong, they should be \r\n, not just \n.