If i use netcat to listen to a port like so: nc -l 5555, I can then go to localhost:5555 in my web browser and netcat will print the request. However, if I click refresh in my browser, netcat stops working. What is causing this?

I've found that specifying -k forces netcat to listen after the current connection completes, but I'm wondering what makes it close by default.


It closes because the browser opens a TCP connection, performs the HTTP transaction, and then says "I'm done!" which usually will close the connection. As you discovered, the -k switch will keep the listening socket open for further connections.

  • What is the part of the TCP request that says "I'm done"? Is it something like Ctrl-D? That is how I close the connection in Bash. – jabe May 24 '18 at 21:20
  • 3
    It is in the HTTP protocol, by default previously header Connection: close so the server shutdowns the connection after its reply since the client will open a new connection. But the client can request the connection to be left open for future requests (the server may agree or not since it can not obviously keep "too many" pending ones), with the "keepalive" value instead of "close". Things change quite a lot in HTTP/2. – Patrick Mevzek May 24 '18 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.