1. Overview
The Transmission Control Protocol (TCP) is a connection-oriented protocol corresponding to the fourth layer of the OSI model. Handling multiple TCP connections is an essential task for servers. By listening to many connections, servers can scale up to sequentially or simultaneously communicate with numerous clients.
In this tutorial, we’ll explore how we can set up several TCP connections over the same socket using netcat.
2. Multiple Sequential Connections
We can establish sequential TCP connections over the same port using a networking tool such as netcat.
2.1. The nc Command
netcat is a versatile networking utility that we can use to set up a client-server connection over TCP (default) or UDP. For example, we can use the connection to transfer files, exchange data, or chat over the network.
In Linux, the netcat-openbsd package provides a common implementation of netcat via the nc command. However, nc doesn’t allow for multiple parallel connections over the same port. On the other hand, sequential connections are possible with nc.
2.2. Sequential Connections Using nc
We can set up multiple sequential connections using the -k option of nc:
$ nc -k -l -p 1234
In this case, we start a server process that listens for incoming connections on port 1234. The -l option instructs nc to listen for incoming connections. The -p option specifies the port number which nc listens to. Finally, the -k option instructs nc to keep listening for other connections once the current connection is closed.
Now, we can connect to the server process from another machine or through a second terminal on the server machine. We’ll create two client processes, Client-1 and Client-2.
So, assuming the server’s IP address is 192.168.1.67, we can connect to it from Client-1 via nc:
$ nc 192.168.1.67 1234
one
two
The server and Client-1 processes are now able to send messages back and forth over the established connection. In this case, for example, we input two lines of text in Client-1’s terminal, and consequently, these also show up in the server’s terminal.
Let’s add our Client-2 process by connecting to the server process from another terminal:
$ nc 192.168.1.67 1234
three
four
Because nc doesn’t allow for simultaneous connections over the same port, the two lines of text in this case won’t show up in the server’s terminal until the Client-1 process is terminated, for example, via Ctrl+C.
At that point, the server and Client-2 processes can start to communicate back and forth.
3. Multiple Simultaneous Connections
We can set up several concurrent connections by either creating multiple nc listening processes or using the ncat command.
3.1. Using Multiple nc Processes
Although nc doesn’t allow for concurrent connections over the same port, we can still create such connections by initiating multiple instances of the nc command:
$ for i in {1..2}; do nc -l -p 1234 & done
In this case, we create two listening processes over the same port with the number 1234. The processes run in the background as indicated by the ampersand symbol (&).
Now, we can create two client processes across two separate terminals, exactly as we did earlier. Both Client-1 and Client-2 terminals can send messages back and forth with the server terminal. Thus, their messages appear on the server-side, and any message entered in the server appears in both Client-1 and Client-2.
Notably, once the two connections with the server conclude, no more connections will be accepted.
3.2. Using ncat
Setting up multiple nc listening processes may not be resource-efficient. So, we can use the NMAP project implementation of netcat known as ncat. The ncat command offers more advanced features compared to nc. One such feature is the ability to establish multiple concurrent connections over the same port.
First, we start an ncat server process:
$ ncat -k -l -p 1234
The options used in this case are similar to those of nc. However, the -k option here allows for keeping multiple connections open, simultaneously.
On the client side, we can now use either ncat or nc to establish a connection to the server over port 1234.
First, we create the Client-1 process and send two lines of text:
$ ncat 192.168.1.67 1234
one
two
Then, we create the Client-2 process and send two other lines of text:
$ ncat 192.168.1.67 1234
three
four
Both Client-1 and Client-2 terminals can now exchange messages back and forth with the server simultaneously.
4. Brokered Mode Connections
Additionally, ncat provides an option for establishing multiple concurrent connections to a server while brokering communication among multiple clients. This means that ncat acts as a centralized server enabling several clients to communicate back and forth with each other.
To test the setup, we start by creating the server process:
$ ncat --broker -l -p 1234
The –broker option of ncat enables brokering mode for all connections made to the server.
Let’s connect to the ncat server from Client-1:
$ nc 192.168.1.67 1234
Likewise, we create a Client-2 process from another terminal and input two lines of text:
$ nc 192.168.1.67 1234
one
two
The Client-1 and Client-2 processes can now communicate with each other as if they’re connected directly. Any lines of text entered in the Client-1 terminal appear in the Client-2 terminal and vice versa, whereas nothing appears in the server’s terminal.
The server only brokers communication between the clients connected to it.
5. Conclusion
In this article, we explored several ways to listen for multiple TCP connections over the same socket using different implementations of netcat. In particular, we can create multiple sequential connections using nc with the -k option. Once a connection is closed, any next one on the same port takes its place.
For concurrent connections to the server process, we can use the ncat command with the -k option. Finally, for brokered mode connections, where clients can communicate with each other, we can use the ncat command with the –broker option.