I need to use nc in a weird way, where I want the server to first send a file, and then recieve a file, and this repeats. I would like to have every instance of the server server close after some seconds. How can I do this in a script? I already have a client that can interact with the server.


Some netcat implementations have a timeout option. For the one on my system, it is -w. Thus

$ nc -l -p 2000 -w 5

will listen on port 2000 and exit in 5 seconds unless a connection is attempted on that port.

  • most nc's suck these days, though, because they are not kept. ncat - by the nmap people - is the exception, i think. and, of course, there is socat if you have the determination to learn a new programming language. – mikeserv Jan 5 '16 at 15:06
  • This is incorrect. According to man nc(1): "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" – Daniel Trugman Mar 17 at 11:38

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.