Home > Back-end >  How close connection in netcat in a shell script after a pipe?
How close connection in netcat in a shell script after a pipe?

Time:09-09

I want to write a script to test my server and I want to be able to connect and disconnect thousands of users in a script that uses nc, what I came up with is:

echo "Test" | nc localhost <port> &
NCPID=$!
sleep 1
kill -kill $NCPID

But I'd like to remove the sleep 1 and still get the netcat connection closed after "Test" was echoed, how could I do that ?

CodePudding user response:

Check -w option for netcat:

-w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag. The default is no timeout.

echo "Test" | nc localhost <port> -w 1 &
  • Related