0

I was looking to learn more about web servers and so I followed this tutorial on codeguru to build a sample one. However, whenever I run it and load the default page, I get the standard GET http request and then another socket connection is accepted and then a blank http request is shown. The console output is below:

Web Server now listening on port 7070, press ^C to stop...
Socket Type: Stream

Client Connected!
=================
Client IP: 127.0.0.1:56310

Message received: "GET / HTTP/1.1
Host: localhost:7070
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  "
Directory requested: /
File Requested: C:\_WebServer\default.html
No. of bytes sent: 103
Total Bytes: 191
No. of bytes sent: 191
Socket Type: Stream

Client Connected!
=================
Client IP: 127.0.0.1:56311

Message received: "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "
Only GET is supported :(

What is this blank http request and why is there a second socket connection even though I only made one request from my browser? Is this a sort of keep-alive thing?

Edit: Could this maybe be a timeout thing?

Edit2:

I think a reason for this might be the following:

server socket receives 2 http requests when I send from chrome and receives one when I send from firefox

It seems like Chrome does a bit of a "placeholder" socket request to optimise later transfers. I get this behaviour in IE as well though, so maybe IE is now doing something similar.

Community
  • 1
  • 1

1 Answers1

0

That code has so many errors that I would not recommend anyone reading it. The most severe is that it expects everything to be sent and received with a single operation which is just true for the most trivial operations.

If you want to learn how to code a web server I suggest that you look at my code: https://github.com/jgauffin/Griffin.Framework/tree/master/src/Griffin.Framework/Griffin.Core/Net/Protocols/Http

It's released under the apache license and I've separated the HTTP protocol parsing from the network IO which hopefully makes it easier to understand than the article that you linked to.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks for the input. The page I linked was useful for getting a (very barebones) understanding of what exactly it is that a web server does in the most basic sense of a web server. I'm not looking for anything fancy (just looking to learn), but I'll definitely check your work out and see if I can make sense of it. – Corey Maddie Besmer Jun 12 '14 at 03:55