6

I'm using nc in a Debian environment:

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

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 192.168.1.185 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?

2
  • What is exactly the question? What is the intended use? Commented Jan 6, 2019 at 9:02
  • "Is there a way to have a timeout less than 1 second with nc?" Commented Jan 6, 2019 at 9:50

1 Answer 1

7

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 192.168.1.185 8800
6
  • 1
    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. Commented Jan 6, 2019 at 13:24
  • 1
    @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. Commented Jan 6, 2019 at 15:29
  • 1
    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... Commented Jan 7, 2019 at 16:03
  • 1
    @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. Commented Jan 7, 2019 at 21:38
  • 1
    This is very useful for control of benchtop laboratory instruments, such as power supplies. It helped me in my Q&A here. Thanks. Commented Nov 17, 2021 at 23:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.