954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Web Server wont return image

Ok so ive been writing code for a web server to return requested files, It works well for html and txt files but when an image is requested all i recieve is the url of the file... In firefox in conqueror i dont get anything. All i do is read the file and send it that is all so that part is fine. I think the problem is somewhere in the headers of the response of the server.

const char *r1="HTTP/1.1 200 OK\r\n";
	const char *r2="Date: ";
	const char *r3="\r\nConnection: close";
	const char *r4="\r\nContent-Length: ";
	const char *r5="\r\nContent-Type: ";
	const char *r6="\r\n\r\n";
	const char *r7="<Head><Title>Page Found</Title>";
    	const char *r8="<Body> File: ";
    	const char *r9=" was found</body></head>\r\n";
	strcpy(returned,r1);
    	strcat(returned,r2);
	strcat(returned,dates);
	strcat(returned,r3);
    	strcat(returned,r4);
	strcat(returned,ContentSize);
	strcat(returned,r5);
	strcat(returned,ContentType);
	strcat(returned,r6);

	if(isGet=0)
	{
		strcat(returned,r7);
		strcat(returned,r8);
    		strcat(returned,FileName);
    		strcat(returned,r9);
	}
	if(isGet=1)
	{
		fin=open(pname,O_RDONLY);
		frc = read(fin,bufferer,bufsiz);
		strcat(returned,bufferer);
	}	
		
		write(connsock,returned,strlen(returned));
Dizzzy
Newbie Poster
23 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

strcat (at line 31) doesn't work well with binary contents.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

It returns other webpages of huge sizes and txt file great but images it just doesnt seen to do... It returns content type image/jpeg if requesting a jpeg or jpg file and image/gif for gif and image/png for png files it cant undertand it. and pname is just the path of the file with the name

Dizzzy
Newbie Poster
23 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Are other webpages of huge sizes and txt file[s] binary files? I suspect not. strcat() is only useful on text, not binary information.nezachem is correct because an image is binary. You need a different way of processing non-text information.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Shouldn't you fix those if statements to be like ...

if(isGet == 0)
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Good catch on the if statements completely missed that and it still worked for somereason anyway, Ye i changed the strcat so now all it does is read and write

if(isGet==1)
		{
			write(connsock,returned,strlen(returned));
			fin=open(pname,O_RDONLY);
			frc = read(fin,bufferer,bufsiz);
			write(connsock,bufferer,strlen(bufferer));
		}

I still get the same as before the url but not the picture

Dizzzy
Newbie Poster
23 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Mhh! Is Apache and the co not enough? Why reinvert the wheel?

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
Are binary files? I suspect not. strcat() is only useful on text, not binary information.nezachem is correct because an image is binary. You need a different way of processing non-text information.


as long as you can encode '\0' to a different value, it won't be a problem.
I have done this many times,I have wrote my own encoding for binary data.

NicAx64
Posting Pro
536 posts since Mar 2009
Reputation Points: 86
Solved Threads: 43
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: