While troubleshooting the problem described here, I tried something really basic: I tried setting up a very basic netcat connection over UDP, and I noticed it's not working as I expect.
As with the TCP illustration below, I expected the UDP case to echo the same output on Window 1 as I type it in Window 2; instead, I get no output in Window 1 in the UDP case.
I'm running Debian Jessie on my laptop, and I have Wireshark running with the following capture filter:
udp and not (port 123 or port 5353 or port 1900)
In one terminal window, I began with a TCP test by running the following command:
$ nc -l 6900
In a second terminal window, I ran the following:
$ nc localhost 6900
In Window 2, I typed "one" and pressed Enter, then I typed "two" and pressed Enter, and finally I pressed Ctrl+D to exit.
After pressing Enter on "one", I saw "one" echoed in Window 1. After pressing Enter on "two", I saw "two" echoed in Window 1. When I pressed Ctrl+D, both netcat instances exited and I returned to the prompt.
This demonstrates I could get a TCP connection going without any problem. Next, I tried UDP.
Window 1:
$ nc -l -u 6900
Window 2:
$ nc -u localhost 6900
It's funny. I think I could only type "one", press Enter, then "two", and press Enter, and I automatically returned back to the prompt in Window 2.
On Window 1, I don't see any output.
I retried with -v
in Window 1:
$ nc -v -l -u 6900
Listening on [0.0.0.0] (family 0, port 6900)
Funny when I retried with -v
in Window 2:
$ nc -v -u localhost 6900
$
It's like netcat never ran; I returned immediately to the prompt.
Update: If I replace localhost
with 127.0.0.1
, there is progress.
With the "server" listening in Window 1:
$ nc -v -l -u 6900
one
two
three
I was able to get output echoed from Window 2 when typing "one", "two", and "three":
$ nc -u 127.0.0.1 6900
one
two
three
^C
The -v
is still puzzling, because it's the same as above.
Best Answer
Using macOS, I come to a different conclusion. The key is IPv6.
localhost
resolves to both IPv6 and IPv4. However, the following command line will causenc
to listen on IPv4:The result:
Now when you use this to “connect”:
... it actually connects to IPv6. When you use
127.0.0.1
it will not.However, it does not connect, because UDP is connectionless. As such, there is no way of knowing whether the remote end of the connection really exists. As such, it cannot detect it should fall back to IPv4. Your messages will be sent, but there is nothing listening for those messages.
When sending, the following can be observed:
With TCP, it will try to connect on IPv6, determine this isn’t working, and retry with IPv4:
You can force
nc
to use IPv4: