I'm using nc in a Debian environment:

# dpkg -l | grep netcat
ii  netcat-traditional                    1.10-41+b1
# cat /etc/debian_version

Its help page describe the behavior of the -w flag:

-w secs                 timeout for connects and final net reads

So it seems it's accept seconds only (as integer). I need to reduce this timeout because I have to poll a remote device twice per second:

echo -n read_input | nc -w 1 8800

this command is sent by an application. I can only set the console command to be executed.

Of course, with a timeout of 1 second I can barely poll the device about 1 time every two seconds (to avoid to open a new socket when the previous is not closed yet).

Do you confirm there's no way to achieve this? So, is there a way to have a timeout < 1 second with netcat? For my own curiosity: why a network timeout should be in seconds?

  • What is exactly the question? What is the intended use? – Rui F Ribeiro Jan 6 at 9:02
  • "Is there a way to have a timeout less than 1 second with nc?" – Mark Jan 6 at 9:50

I don't think it is possible with nc alone. But you can additionally use the timeout tool (GNU coreutils package) which allows you to run a command with a timelimit specified as a floating point number, e.g. like so.

echo -n read_input | timeout 0.5 nc 8800
  • As far as I know, nmap is not so useful to receive the answer from the device. The application expects on stdout the data to be parsed. With the --data-string I guess I can easily send the TCP payload, but I don't find an easy way to retrieve the output. – Mark Jan 6 at 13:24
  • @Mark OK. I think I don’t understand what you are trying to achieve. I’m sorry. Perhaps it is possible to clarify the question. E.g. clarify what network entities are involved, how they are or should be configured, and which one is under your control. Otherwise, you may have more luck at SE Server Fault. – ozzy Jan 6 at 15:29
  • I apologize. I just trying to send a TCP packet to a remote device, receive the answer and close the socket. Of course I can write a C++ application to do the same, but I'm trying to do this on command line. The only problem is 1 second of timeout is too much... – Mark Jan 7 at 16:03
  • @Mark I revised my answer. It’s still not 100% clear to me what you wish to do, but I guess the ‘timeout’ tool may suit your needs. – ozzy Jan 7 at 21:38
  • Good hint! It seems to work as expected. – Mark Jan 8 at 14:36

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.