Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Receive Image Bytes From HTTP Response Through C Socket Programming, How to?

Status
Not open for further replies.

shinsengumi

Newbie level 4
Joined
Dec 11, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
Hi,

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.
 

after you open a socket ,
you can use the function:

recv (
SOCKET s,
char buf,
int len,
int flags
);

where s--socket opened,
buf ---is the buffer to receive the image data,
len --- is the size of buffer and
flags--keep at 0 as default.

once buffer is filled , you can write it to a file.
 

after you open a socket ,
you can use the function:

recv (
SOCKET s,
char buf,
int len,
int flags
);

where s--socket opened,
buf ---is the buffer to receive the image data,
len --- is the size of buffer and
flags--keep at 0 as default.

once buffer is filled , you can write it to a file.

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top