I wrote this grabimage.c program last year that will download an image into a directory called /root/test if you give it the url of the image on the command-line. It also displays the header of the packet containing the image. (You may also need to look at the header file, mine.h, which is a bunch of generic functions only a few of which are used in grabimage.c)
Unfortunately, I didn't add much in the way of comments, but it may give you some idea of the complexities involved. It works this way:
- create a PF_INET socket and connect() to the remote server
- send a GET request for the image
- while receiving the response, watch for the "Content-Length:" line; this gives you the exact size of the image, and everything after that is the image itself.
- write the image to a local file
One complication I can recall is that your send/receive (or write/read) calls are not guaranteed to write or read as much as you want. This can be a screw up, particularly with the send, so you need to verify and complete the process:
Code:
int Transmit (int sock, char *line) {
int done, todo=strlen(line);
while (todo>0) {
if ((done=send(sock,line,todo,0))==-1) return done;
todo-=done;
line+=done;
}
return 0;
}
This way, if send() only sent half of "line" on the first call, we loop around, advancing the "line" pointer appropriately, until the entire string has gone.