<img height="1" width="1" src="https://www.facebook.com/tr?id=1640437302870650&amp;ev=PageView &amp;noscript=1">

Subscribe to Email Updates

bg-img11.jpg

How to simulate a TCP/UDP client using Netcat

Maria Hernandez
By Maria Hernandez
on July 11, 2017

This piece is here to introduce you to the Server and Client Concept. Firstly, how does the Server/Client concept work within Internet of things? The client is the device that initiates the communication. The server is Ubidots (or any other server connection), that will accept connections and manage the data transfers using a simple protocol like TCP or UDP. This article will show you how to simulate a client using a usefull tool called Netcat and realize the immediate connection using your own computer's terminal.

To learn more about of managing your data with Ubidots, reference 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
Selección_004.png
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:

client-server-connection.png

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

client-server-message.gif

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:

udp-connect.gif

 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 more information, check out this link for additional information. 

Happy Hacking! :) 

Leave a comment

Maria Hernandez
Written by Maria Hernandez
Written by Author

Related posts