1

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 :)

pmerino
  • 5,900
  • 11
  • 57
  • 76
  • You should ask your bundle for the path or URL to the image, instead of stapling partial paths together with string-concatenation methods or `stringWithFormat:`. – Peter Hosey Nov 13 '11 at 00:11
  • Also, in generating your output, you seem to assume that you have a “`contententType`” and `responseBody` regardless of whether you actually found and read the file and consequently assigned them or not. – Peter Hosey Nov 13 '11 at 00:15
  • Hi Peter Hosey. I set my body and headers by default before that code, in case something fails. I also know now that I must send the image as binary data, thanks – pmerino Nov 15 '11 at 14:35

1 Answers1

1
responseBody = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];

Don't treat arbitrary file-contents data as text. It isn't.

A PNG is not an ASCII text file; it contains binary data that you must treat as such. That means staying with NSData the whole way through, and not at any point trying to convert it into or interpret it as text for an NSString.

The only string you should create is for the response code and headers. Generate data from that string, then concatenate the data from the file onto it and send that to the server.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I love you, @Peter Hosey… You obviously know everything (that I want to know), but you're like any other wise, ole dame, you likes to keep'em guessing, lol.. At the end of your posts, I always find myself on the edge my seat… and then you are like do this and that and the other thing.. A.k.a. what it is I wanted know. In this case " Generate data from that string, then concatenate the data from the file onto it and send that to the server.", lol. I'm just busting your chops - I always appreciate that you post back with a more detailed response. cheers. – Alex Gray Aug 05 '12 at 02:20