Sign in to follow this  

Socket Web Server - No Images?

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a basic web server using Winsocket/BSD Sockets for my mmorpg and it displays html files perfectly with one exception, there are no images. What do I need to do in order to get them to be displayed? Is it a content type issue? Thanks in advance. [Edited by - DarkHacker on February 7, 2005 12:29:41 PM]

Share this post


Link to post
Share on other sites
I'm sending the content-length and the content-type now, opening up any non text file in binary and then reading it one byte at a time. Instead of a timeout with an eventual "This image is broken" error now it appears like it's a broken image since it's displaying the can't find the image picture in it's place.

Any ideas?

[Edited by - DarkHacker on February 7, 2005 12:11:08 PM]

Share this post


Link to post
Share on other sites
Debug it like you would debug anything else: isolate it to small, individually testable parts.

Can you get an image (without the HTML) in a browser?
Can you get an image file using "wget" on the command line?
Is it exactly the same as the file you initially served? If not, what corrupted it?
Run Ethereal on your browser getting a real image from a real web server, and compare to yours. What's different that would be interesting?


By the way: serving a single byte at a time is a bad idea. You want to read and write large-ish blocks for performance.

Share this post


Link to post
Share on other sites
Thanks for the tips. I've got it to have the correct width of the image now, but the height is really big and there still isn't an image just the default broken image.

When viewed without the html its only a couple pixels wide and about half as long of a broken image in comparison when it's viewed using html.

Wget says there's a read error at the first byte but it connects fine.

The reason I was only doing 1 byte at a time was for debugging purposes, since I actually get a Mozilla error: "The image cannot be displayed, because it contains errors" when doing it with 1 byte.

It's a very strange error.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Perhaps you're somehow reading the image in some ascii mode and not the raw binary data? But yeah, follow the three suggestions previously given - those'll give you the answer.

Share this post


Link to post
Share on other sites
I'm opening it like this:


if (!arrContentType[idx].binary)
inputFile.open(fileName.c_str());
else
inputFile.open(fileName.c_str(),ios::binary);

if (!inputFile.is_open())
{
sendErrorP(socket, "The file (" + fileName + ") you requested does not exist, check your url");
inputFile.close();
return 404;
}

stat(fileName.c_str(),&results);

sendP(socket,"HTTP/1.1 200 OK\nConnection: close\nContent-Type: " + arrContentType[idx].contentType + "\nContent-Length: " + stos((short)results.st_size) + "\r\n\r\n");

if (!arrContentType[x].binary)
{
while(!inputFile.eof())
{
inputFile.getline(aBuf,255);
sendP(socket,aBuf);
}
}
else
{
for (unsigned short x = 0; x < (unsigned short)results.st_size;x++)
{
inputFile.read(bBuf,1);
sendP(socket,bBuf);
}
}

inputFile.close();



sendP is just a wrapper for send so I dont have to have the size parameter, it just does a size of before it sends.

[Edited by - DarkHacker on February 7, 2005 2:55:27 PM]

Share this post


Link to post
Share on other sites
Quote:
Wget says there's a read error at the first byte but it connects fine.


That's a clue right there.

First, make sure that you actually take the "binary" path in your code. Insert logging or breakpoints or whatever.
Second, the header fields should be separated by \r\n, not just \n.
Third, get Ethereal (it's free) and compare your program, with a real server. Really.


Also, why is there an array of elements? In HTTP, each element is its own request. Are you perhaps only serving pre-loaded items, and calculate the index into the array based on the request parameters? If so, are you sure the right thing is being served? Again, insert logging into your code to see that the right thing happens.

Share this post


Link to post
Share on other sites
After some testing it is going down the binary path. I updated the header fields, thanks for that info.

The array holds the content-types, like "image/gif" or "text/html". A couple lines above the code I showed it matches the file extension to the content type that's defined in a seperate file. A lot like the AddType command in Apache's conf files.

I'm getting Ethereal right now I'll report back on my findings.

Share this post


Link to post
Share on other sites
I got it to work!


for (unsigned short x = 0; x < (unsigned short)results.st_size; x++)
{
bBuf = inputFile.get();
sendP(socket,bBuf);
}




I'm using that code for both binary and ascii files, yeah it's only 1 byte at a time but it works. There is one issue it's printing "ÿÿÿÿÿÿÿÿÿ" at the end of the file, I'll have to find the bug that's causing that, shouldn't be too hard.

Thanks your help, this networking stuff is pretty new to me still and there wasn't any examples of a simple web server to look at.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this