Linux: how to end to end verify UDP and TCP connectivity with nc

Testing end to end connectivity in Linux is always a necessity when we want to debug connectivity issues. Before we blame the application…

Linux: how to end to end verify UDP and TCP connectivity with nc
Photo by John Barkiple on Unsplash

Testing end to end connectivity in Linux is always a necessity when we want to debug connectivity issues. Before we blame the application or its configuration we should always verify that there is nothing in the between communication that blocks the connection.

Testing TCP connections

Most likely you know how to test troubleshoot TCP connections already using telnet, telnet is good for this job unless there is no listening application to handle the request but you still have to verify that there is no network configuration problem. nc act as both the server and the client, it can bind in an interface/port and is ready to receive connections from a client (which can be nc again)

To setup nc to listen for incoming TCP connections the generic syntax isnc -l <local_ip> <local_port>

To setup nc to connect to an IP address in a specific port the generic syntax isnc <destination_ip> <destination_port>

Example: TCP Server / Client

We configure the “server” to listen on all interfaces and port 55000nc -l 0.0.0.0 55000

We configure the “client” to connect to IP of host server1.test.com and port 55000nc server1.test.com 55000

Now on the “client” if we type “hello\n” and press enter we should see the message on the “server” screen. if not probably there is a network problem between those “server” and “client”

Example: UDP Server / Client

We configure the “server” to listen on all interfaces and port 55000, the only difference from the previous example is -u parameter which instructs nc to listen/connect to a UDP socket.nc -u -l 0.0.0.0 55000

We configure the “client” to connect to IP of host server1.test.com and port 55000nc -u server1.test.com 55000

Now on the “client” if we type “hello\n” and press enter we should see the message on the “server” screen. if not probably there is a network problem between those “server” and “client”

I hope you found this article useful and help you troubleshoot networking problems easier ;)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…