-1

I'm creating a very simple http server for an assignment for my course.

It will handle GET and HEAD requests appropriately.

So far, My server sees the clients connect, and sends the same response to each of them (intentional, for testing).

if I don't give the clients a response (and leave the web browsers loading), and then close the web browser (as the page is loading), my server sees that the client disconnected.

However, when I send a response to them (which is just a status line, 2 or 3 header fields, and some html to give the web pages a title), they successfully display the web page (just the title for now, as intended), but my server doesn't see them disconnect anymore when I close the web browser/tab.

Here is the response I'm sending to all the clients:

char resp[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 53\r\nConnection: keep-alive\r\n\r\n<html><head><title>Dan's Server</title></head></html>\0";

Note: I write strlen(resp) bytes to the socket, i.e I don't write the final zero byte to the socket.

Another note: Something else I noticed when testing just now was that if I connect 2 clients 1 after the other (my server is multi threaded), the second connection doesn't show up as a new one. This only happens when I respond to them though (weird?). If I don't respond, my server sees them as separate connections.

halfer
  • 19,824
  • 17
  • 99
  • 186
toastedDeli
  • 570
  • 5
  • 28
  • You're telling the clients to stay connected by sending `Connection: keep-alive` :) – Seva Alekseyev Oct 22 '17 at 21:00
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 23 '17 at 12:26

1 Answers1

3

The client probably asked for a HTTP keep-alive connection, i.e. that the TCP connection should stay open for further requests after the response for the current request is received. All current browsers use HTTP keep-alive by default.

Your response agrees to this keep-alive. This means that the client will not close the connection immediately since you agreed on keeping the connection open for further requests.

Your options are:

  • close the TCP connection to the client yourself
  • ask the client to close the connection by sending Connection: close as HTTP header instead of the current Connection: keep-alive.

Note that an understanding of the HTTP standard is actually recommended when writing your own HTTP server. HTTP is more complex than you might think when looking just at some example requests. To study the standard start here.

Community
  • 1
  • 1
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Any idea as to why the server thinks the clients are the same when they connect after getting a response? see my "another note" – toastedDeli Oct 22 '17 at 21:00
  • @toastedDeli: I have no idea of your code so I can only guess what is happening. If these "2 clients" are actually the same browser then the browser is reusing the existing connection. But the connection can only be reused if there are no outstanding responses which might explain what you see. – Steffen Ullrich Oct 22 '17 at 21:02
  • So keep-alive will actually make the browser maintain the connection for a while, even after closing the tab that was connected to my server? – toastedDeli Oct 22 '17 at 21:08
  • 1
    @toastedDeli: that's what HTTP keep-alive is for: reduce the cost of a request by keeping the TCP connection to the server open for more requests since the setup of a new TCP connection takes time. Again, you should have a closer look at the standard instead of guessing behavior. That's what standards are for. – Steffen Ullrich Oct 22 '17 at 21:11
  • @toastedDeli - most browsers share HTTP connections (HTTP/2 and HTTP/1.1) among different tabs. This "global server connection" allows different tabs to reuse existing connections and speed things up. This usually improves performance since establishing new connections is "expensive" and TCP/IP has a "slow start" feature built in, making connection speeds slower when they are first established (a feature aimed at minimizing lost packets). – Myst Oct 22 '17 at 23:10