samedi 22 avril 2017

Make netcat discard all bytes after disconnect

I use netcat to run a tiny development webserver through the bash, which is able to handle one connection at once.

Netcat is started as follows (-k to make it survive multiple connections):

nc -kl 3000

A bash script processes the browser's request which is received by netcat, and builds the response, which is sent back to the browser through the same netcat instance.

Everything works as expected, but sometimes, the browser does not get the file it requested. My suspicion: If a connection is closed before the response is sent completely, the response's remainder is sent as response to the following request (it, of course, not belongs to).

Proof

  1. Terminal 1 (Server): nc -kl 3000
  2. Terminal 2 (simulates Browser): nc localhost 3000
  3. Type hello\n in terminal 1.
  4. Terminal 2 prints hello\n.
  5. Do Ctrl+C in terminal 2 to end the connection.
  6. Type world\n in terminal 1.
  7. Run nc localhost 3000 in terminal 2 again (new connection).
  8. Terminal 2 immediately shows world\n even though world\n was actually sent when no connection existed, meant as second response line in the first connection.

Required behavior: Ignore all bytes that were passed to netcat if no connection exists.

Is it possible using netcat? (I prefer a tool like netcat as it comes pre-installed on all machines.)




Aucun commentaire:

Enregistrer un commentaire