Occasionally it’s useful to determine the maximum speed of the network connection between two machines. I’ve used the following method on several occasions, generally as a method of troubleshooting. This quick howto will demonstrate a method to test the speed of a connection between two computers, using a very simple mechanism — netcat.
netcat is a very simple, but incredibly useful, utility that can be used to send or receive data over a network. When executed with the -l
flag, netcat enters listening mode, and can be used to receive data on a specified port. Without the -l flag, netcat will send data to a specified host and port.
Using netcat is simple. If you have two Linux machines, you’ll want to setup one of them to listen, and the other to send. On the machine where you want to listen, execute something like the following:
This tells netcat to open port 20,000 and listen for connections. If executed successfully, it will simply sit on the command line and do nothing.
On your second machine, tell it to connect to the machine that is setup to listen. If the first machine has an IP address of 192.168.1.50, for example, you’d use the following, assuming you set netcat to listen on port 20,000:
If it has connected successfully, it will simply wait on the command line and do nothing. You can test whether or not the connection is working by typing some data into the sending machine, and pressing enter. The text you’ve entered will pop up on the receiving machine. Press control+c to quit netcat.
This simple demonstration shows why netcat can be such a useful tool; all it is actually doing is taking the standard input from one machine and transferring it to the standard output on another. You can use netcat in this way to transfer all sorts of data (although other protocols are more useful for things like typical file transfers).
Once you’ve got the basics down, you can begin testing your network bandwidth with netcat. To do this, we’ll want to have a source of input data that is known to be faster than the network, to make sure we aren’t bottlenecked on the input. The simplest way to guarantee that your input isn’t the bottleneck is to use /dev/zero as the input, which will create data as fast as your processor can stand. We’ll want to make sure the same is true on the receiving end, so we’ll use /dev/null as a destination target. In this way, we can make sure that the only bottleneck in the test is the network.
To get the data into and out of netcat, we’ll use the dd command. Put all together, the listening server should look like this:
And the sending server should look like this:
Of course, replace the IP addresses and ports to match your network.
While the test is running, you won’t be able to see any statistics or data transferring. And, as written, the test will run forever; you can limit the amount of data sent by specifying a block size and count for dd, like so:
This command will transfer 30MB of data over the network (a blocksize of 1MB, and 30 blocks, is 30MB). Once all data is transferred, you’ll get the output from the dd command, showing throughput:
30+0 records in
30+0 records out
31457280 bytes (31 MB) copied, 5.40742 s, 5.8 MB/s
Another option for measuring the results is to use a traffic monitoring application such as iftop. You may wish to read How To Monitor Network Traffic in Linux, which provides a nice overview of iftop and similar applications, to learn more about monitoring network traffic.
You can use netcat to easily transfer data over a network, which makes it an incredibly powerful tool. Testing network throughput is just one of its many uses; I encourage you to read more about netcat, as once you know how to use it, you’re certain to find occasion to take advantage of it.