What command/technique is needed in a C program to receive image bytes through socket programming from an HTTP response? I want to develop a program that requests for an image from a server, then upon receiving the bytes comprising the image, it should write these bytes into a newly created file such that an image file is created. The image is of .jpg format. I really don't know how to create a program that receives and stores bytes through sockets so I hope someone here knows how to do it. So far, what I know is how to receive arrays of characters (strings) but now I want to know how to receive image bytes.
Also, the response my program is receiving has an HTTP header that comes before the actual image bytes so I need to remove the HTTP header first before writing the image bytes into a file. How do I do it? Thanks in advance. I'm working on a Windows XP machine.
That's actually what I've done. The problem is, the HTTP response I'm getting seems to be the HTTP header only (no image bytes.) After receiving the response, I issue a print command to see what I'm able to receive:
Code:
numbytes=recv(ConnectSocket,recvbuf,sizeof(recvbuf),0);
if (numbytes == SOCKET_ERROR)
printf("Server: recv() error %ld.\n", WSAGetLastError());
else {
printf("Server: Received data is: \"%s\"\n", recvbuf);
printf("Server: Bytes received: %ld.\n", numbytes);
fwrite(recvbuf, sizeof(recvbuf), 1, fpic);
fclose(fpic);
}
The following output is printed on the screen:
Code:
Server: Received data is: "HTTP/1.1 200 OK
Date: 01 Jan 1970 00:01:14 GMT
Content-Type: image/jpeg
Content-Length: 10046
Content-disposition: filename="something.jpg"
Connection: close
"
Server: Bytes received: 225
The response above is just the HTTP header. I'm sure of this because I'm using a software that analyzes packets that is transmitted to/from my PC. And I can see the response above in the packets that my PC receives, and following this response are bytes comprising the image itself. The problem is that my program doesn't seem to receive these bytes.