The Linux command line allows you to retrieve data by listening on a socket or by connecting to a socket. The data can be recorded in a text file. We show you how.
Socket clients and servers
Sockets allow network software to communicate. They were first implemented in the 4.2BSD Unix operating system, which was created in 1983 at the University of California, Berkeley. They were quickly adopted by System V Unix and Microsoft Windows.
A socket is an endpoint of a software network connection, abstracted so that it can be treated as a file handle. That means it fits the general Unix and Linux design principle of “everything is a file”. We don’t mean the physical outlet you plug your network cable into.
If a program connects to a socket on another piece of software, it is considered the client of the other software. Software that allows other software to request connections is the server† These terms are used independently of other client and server usage in the IT world. To avoid confusion, they are sometimes: socket client and socket server to remove ambiguity. We’re going to call them clients and servers.
Sockets are implemented as an Application Programming Interface (API), allowing software developers to call socket functionality from within their code. That’s fine if you’re a programmer, but what if you’re not? Or maybe you do, but your use case doesn’t justify writing an application? Linux provides command-line tools that allow you to use socket servers and socket clients to retrieve or receive data from other socket-enabled processes, according to your needs.
RELATED: What is an API and how do developers use them?
Relationships are never easy
The programs we will be using are: nc
and ncat
† These two utilities have a strange relationship. The nc
program is a rewrite of ncat
who is much older than nc
† But ncat
has also been rewritten, and it now lets us do some stuff nc
can not. And there are many implementations of ncat
which itself is a derivative of a tool called netcat
† In addition, on most distributions, nc
is a symbolic link to ncat
and no separate program.
We’ve checked recent Arch, Manjaro, Fedora, and Ubuntu distributions. The only one that required the tools to be installed was Manjaro. On Manjaro you must use the . to install netcat
package to get nc
but you don’t get it ncat
You get netcat
† And on Manjaro, nc
is a symbolic link to netcat
I
sudo pacman -S netcat
The bottom line is how to use Manjaro netcat
When you see it ncat
in the examples in this article.
Listening through a wall outlet
When software listens for incoming socket connections, it acts as a server. All data coming through the socket connection is: have received by the server. We can replicate this behavior very easily using nc
† All received data is displayed in the terminal window.
we have to tell nc
to listen for connections, using the -l
(listen) option, and we need to specify the port on which we are going to listen for connections. Any client programs or processes that attempt to connect to this instance of nc
must use the same port. We tell nc
on which port to listen with the -p
(port) option.
This assignment begins nc
as socket server, listening for a connection on port 6566:
nc -l -p 6566
While you are waiting for an incoming connection, nc
produces no output. Once a connection is established, all retrieved information will be displayed in the terminal window. Here is a connection made by a client program that identifies itself as ‘client 1’.
All displayed by nc
is received from the client. This customer happens to send his name and a numbered message with the time and date.
If the customer disconnects, nc
ends and you are returned to the terminal prompt.
Send data to a file
To capture the customer’s data in a file, we can send the output of: nc
to a file using redirection. This command saves the received data in a file called “logfile.txt”.
nc -l -p 6566 > logfile.txt
You won’t see any output – it comes in the file – and, paradoxically, you won’t know if a connection has been made until nc
ends. Returning to the command prompt indicates that a connection has occurred and has been terminated by the client.
we can use less
to view the contents of the “logfile.txt” file.
less logile.txt
You can then browse and search the data using the built-in features of less.
RELATED: How to use the less command on Linux?
Send data to a file and the terminal window
To watch the data scroll past in the terminal and have it sent to a file at the same time, pipette the output of nc
go inside tee
I
nc -l -p 6566 | tee logfile.txt
Accept multiple connections
That’s all fine, but it does have limitations. We can only accept one connection. We are limited to receiving data from a single customer. Also, when that client disconnects, our socket server nc
ends.
If you need to accept multiple connections, we need to use ncat
we’ll have to tell ncat
to listen and use a particular port, just like we did with nc
† But we also use the -k
(keep alive) option. This tells ncat
to continue and accept connections from clients even if the last active connection drops.
This means ncat
will run until we choose to end it with “Ctrl-C”. New connections are accepted or: ncat
is currently connected to all clients or not.
ncat -k -l -p 6566
We can see the data from the different clients appear in the output of: ncat
when they connect.
Connecting to a server
We can also use nc
as a socket client and connect to another program that accepts connections and acts as a server. In this scenario, nc
is the socket client. To do this, we need to tell: nc
where the server software is located on the network.
One way to do this is to provide an IP address and port number. If the server is on the same PC we are using nc
we can use the loopback IP address of 127.0.0.1. Not that flags aren’t used to indicate server address and port number. We only give the correct values.
To connect to a server on the same PC and use port 6566, we can use the loopback IP address. The command to use is:
nc 127.0.0.1 6566
data that nc
retrieving the server passes in the terminal window.
If you know the network name of the computer running the server software, you can use that instead of the IP address.
nc sulaco 6566
Use “Ctrl + C” to disconnect.
Fast and easy
nc
and ncat
fits well if you don’t want to write a custom socket handler, but you need to collect data from some socket enabled source. By redirecting the output to a file, you can view the output with less
and parse the file using tools like grep
I
RELATED: How to use the grep command on Linux