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));

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

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

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.

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

if(isGet == 0)

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.