Testing network connection Link to heading

Image

Here is a very simple way to test network connection between two computers. This is useful when testing, for example, whether your computer behind your router can be reachable from external IP with port-forwarding. In fact, this was exactly the scenario I was testing.

I have a Raspberry Pi on my home network behind a router. I configured my router to assign static IP to my Raspberry Pi and exposed one of its ports to the outside world via port-forwarding. Now, I wanted to test whether I can establish a connection initiated from a device outside of my router to the Raspberry Pi device. Here is how you can test it very easily.

# on the listening device, i.e., raspberry pi behind the router
$ nc -l 0.0.0.0 PORT_NUMBER

Here, PORT_NUMBER is obviously the port you want to use. On a different device which is connected to an outside network, i.e., a laptop using my mobile’s hotspot

# from a device outside of the home network
$ nc -v EXTERNAL_IP_ADDRESS PORT_NUMBER

Here, EXTERNAL_IP_ADDRESS is the IP-address of the home-network seen from the outside, and PORT_NUMBER is the same port number used with the listener. If the connection is successful, you will be able to send text messages between the two devices!

By the way, I was having some trouble establishing the connection even when the port-forwarding was properly configured. It turned out that I was testing the connection from two devices within my home network, and my router does not support NAT hairpinning. That is, two devices within the private network tries to establish a connection using an external IP address. I resolved the issue by connecting my laptop to mobile hotspot and was able to establish the connection with my Raspberry Pi behind the home network.