-
problem sending binary file in a socket
hi everybody,
first sorry for my english i'm french :)
i'm trying to build an http server in c++ and i have some problems.
for simple html pages (no picture, no effects, no javascript), no problem. my server send pages and firefox (or IE) shows it normally.
but when i put a background picture on this html page, the navigator send me 2 requests:
- fisrt to ask me the page.
- second to ask me the picture.
no problem for the first request.
for the second request, i open the picture with the standard fonction fopen(url, "rb").
then i put the content of the file in a buffer with fread(buffer,1,sizeof(picture), fic)
next i declare a second buffer called response which will content http headers and the first buffer.
my http header:
HHTP/1.1 200 OK\n
Accept-Ranges: bytes\n
Content-Length: taille\n
Content-Type: image/jpeg\n\n
next:
strcpy(response, httpHeader);
strcat(response, contentOfBuffer);
int sentBytes = send(socket,response,sizeof(reponse),0);
when i'm testing, sentBytes = sizeof(reponse).
that means the server had all sent to the navigator but this one shows binary characters instead of pictures.
can someone help me ?
thank you for your replies and sorry again for my english.
-
do not use strcat. It is for C strings and stops when a zero (0) is encountered in the binary stream. Use memcpy -- it will not stop until size is reached.
for example
int i = 0;
while (string_thing[i++]); //find end of string.
memcpy(&(string_thing[i+1]), pic, size_of_pic_file);
you should know the size when you read it from file, use that instead of trying to make sizeof work. you will be happier.
Last edited by jonnin; 09-28-2005 at 07:26 PM.
-
never sizeof in this context -- the string's size is dynamic, and is determined by the number of bytes before the first '\0'. Sizeof isn't aware of this and besides, it's value is calcuclated at compiler time, probably as the size of the *pointer* (i.e. 4 bytes) rather than the array's length.
Danny Kalev
Similar Threads
-
By jase_dukerider in forum C++
Replies: 2
Last Post: 04-14-2005, 07:48 PM
-
Replies: 3
Last Post: 02-23-2003, 03:05 AM
-
By Phil Weber in forum .NET
Replies: 3
Last Post: 06-16-2002, 10:50 PM
-
Replies: 1
Last Post: 03-27-2002, 09:01 AM
-
By priya in forum ASP.NET
Replies: 1
Last Post: 01-17-2002, 10:12 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|