I'm making an app which includes a built in HTTP server. I need to send a PNG image over the socket, but somehow it's getting messed and the image just doesn't shows up. This is my code (I'm using AsyncSockets):
if(processingURL == FALSE) {
NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Contents/Resources/public%@",[self getApplicationPath], [request objectAtIndex:1]];
//NSLog(@"%@", filePath);
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
contententType = @"image/png";
responseCode = @"200 OK";
// responseBody = [[NSString alloc] initWithData:[[NSFileManager defaultManager] contentsAtPath:filePath] encoding:NSASCIIStringEncoding];
NSData * content = [[NSData alloc] initWithContentsOfFile:filePath];
responseBody = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];
NSLog(@"%@",responseBody);
}
}
data = [[NSString stringWithFormat:@"HTTP/1.1 %@\nContent-Type: %@\n\n\n %@", responseCode, contententType, responseBody] dataUsingEncoding:NSUTF8StringEncoding];
[sock writeData:data withTimeout:-1 tag:0];
What I'm doing wrong? I think the problem is the response, which is malformed, but I don't know, thanks :)