All Collections
Developer Guides
Sending TCP/UDP packets using Netcat
Sending TCP/UDP packets using Netcat

What is Netcat? And, how to use it to send TCP/UDP packets to a remote server

David Sepúlveda avatar
Written by David Sepúlveda
Updated over a week ago

There are a number of protocols powering the Internet of Things. Choosing the right one will depend on your project's security, bandwidth and reliability needs, or maybe just your device computing limitations. In some cases like cellular transmission, the good old TCP/UDP packet transmission will work just fine. This article will show you how to simulate a client using a useful tool called Netcat and realize the immediate connection using your own computer's terminal.

To learn how to send data to Ubidots using these protocols, see Send Data to Ubidots over TCP or UDP.

What is Netcat?

Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol. Designed to be a reliable "back-end" tool, Netcat can be used directly with other programs and scripts to send files from a client to a server and back. At the same time, it is a feature-rich network debugging and exploration tool that can specify the network patameters while also establishing a connection to a remote host via a tunnel. 

Although Netcat can do many things, its main purpose and most desirable function is to:

  1. Create an initial socket to establish a connection from server to the client.

  2. Once connected, Netcat will automatically generate a second socket to transmit files from the server to the client and visa versa. (This is the really cool part.)

Reference below for a diagram of the data Netcat protocol architecture.

Something so simple happens to be extraordinarily powerful and flexible as you will see below. For simplicity, local connections are used, although, of course, they can be used between different machines. 

 

 

Syntax

nc [-options] hostname port[s] [ports]
nc -l -p port [-options] [hostname] [port]

Basic parameters

  • -l:  set the "listen" mode, waits for the incoming connections.

  • -p: local port

  • -u: set the UDP mode

 

Test your Netcat understanding as a client-server

Open two computer terminals, the first will act as the server and the second will be the client.  

TCP client

With Netcat your PC can be converted in a server, you want to begin as a server that listens at port 2399:

$ nc -l 2399

In addition, we can use the server to connect to the port (2399) recently opened, from the client side:

$ nc localhost 2399

As you can see on the image below, the connection is established:

With the connection established you are now able to write to the server from the client:

$ nc localhost 2399
Hello Server

In the terminal where the server is running, your text files will appear seamlessly.

$ nc -l 2399
Hello Server

UDP client

By default Netcat  uses the TCP protocol for its communications, but it can also UDP using the  -u option. 

As we mentioned at the previous step, Netcat lets you convert your PC in a server. In this case we're going to establish the connection between the server and the client but using UDP.

From the server side,  run the command below. As you can see, the command establishes the UDP connection just requires the -u  to  be added to the command:

$ nc -u -l 2399

Once you start the server, establish the connection with the client:

$ nc -u localhost 2399

Now the client and the server are using UDP protocol for their communication. You can verify commincation using the netstat command in a new (3rd) computer terminal. 

 $ netstat | grep 2399
udp 0 0 localhost:2399 localhost:57508  ESTABLISHED

As you can see in the images below, the message is received by the server, and the transmission is verified by the connection:

 With this introduction to Netcat, you now have a better understanding of this advanced tool to send data quickly and efficiently between client and server. For additional information, check out this link

Happy Hacking! :) 


This article was originally published on Ubidots' Blog on June 22, 2017.

Did this answer your question?