2

A simple example, here's my server/host:

nc -ul 192.168.0.2 54 

And the client:

echo "test" | nc -u 192.168.0.2 54

The server will intercept the "test" message, but nc doesn't close. It just remains open until I press Enter or Ctrl+c. Is it possible to have it terminate automatically? Or perhaps include something in the clients message to signify that the transmission has ended?

nc --version
Ncat: Version 7.70 ( https://nmap.org/ncat )

I'm using Debian and macOS.

1

While TCP has a concept of connections that have a beginning and an end, UDP does not have that. UDP is a connectionless protocol: an UDP receiver simply waits for incoming packets in a specified UDP port and outputs the content of any arriving UDP packets.

The receiving nc has no clue about how many packets might be incoming, nor whether those packets should all be coming in from the same host, or from several hosts: it simply receives anything that arrives to the specified UDP port and outputs the contents of the UDP packets to standard output without parsing it in any way until the nc process is interrupted.

There is not even any built-in guarantee that any packets sent by the client/sender are received in the same order at the receiver end: packets might be lost or reordered on the way. UDP makes no attempt to fix that: it's the job of the thing that is using UDP to deal with all those issues (or ignore them) as appropriate for its purposes.

You might decide on a specific string, make it mean "end of data" and arrange for another script at the listening end to parse the incoming data and kill the listening nc process when that string is received. In that way, you'll start building your own (perhaps very simple) protocol on top of UDP. And that is exactly what UDP was designed for.

In a nutshell, the answer to "Is it possible to have nc -ul terminate automatically?" is "No, unless you make something that does it."

1

netcat has the option -W:

-W recvlimit    Terminate after receiving a number of packets

So:

nc -ulW 1 192.168.0.2 54

But you are using ncat. You can use this workaround:

nc -ul 192.168.0.2 54 -c 'dd count=1 bs=100000 >&3' 3>&1

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.