TCP/IP connection test
Sometimes you need to test whether two machines can connect with each other using TCP/IP. I personally need to do this when debugging client/server applications to rule out network issue. There is a useful command tool called nc.
To test the connection from client B to server A, you need to start nc listening to a port on server A:
nc -l 3080
If you have firewall enabled on your server, you might want to modify firewall rule to allow port you are testing. In this case, the port we are testing is 3080.
Next step is to run nc on client B to connect to server A:
nc 16.53.129.92 3080
The first parameter to nc command is the IP address (or domain name) of server A, the second parameter is the port on which we started nc command listening to on server A.
If the connection is successful, you can type some message in the console on client B. And the nc command on server A will echo the message, like below (we are testing on localhost)
On client B:
macbook-2:$ nc localhost 3080
hello from client B
echo from server A
On server A:
macbook-2:$ nc -l 3080
hello from client B
echo from server A
You can type ctrl+c to end the testing on client B. Note this will also automatically end nc process on server A.
If the connection from client B to server A is not successful, nc won’t wait you to input message and will just exit. If the domain name cannot be found or the port is out of valid range, nc will print corresponding error messages.