Search notes:

Shell command: nc

nc (or netcat) is a shell command for «just about everything under the sun» involving TCP, UDP or UNIX-domain sockets.
nc can be used, for example, to check if if a TCP port is open:
$ nc -vz server.xy 443

Formulate a simple HTTP request

echo -e "GET / HTTP/1.0\r\nHost:renenyffenegger.ch\r\n\r\n" | nc renenyffenegger.ch 80
Note the -e option for echo which enables the interpretation of the backslash in \r\n.

Act as server

By default, nc acts as a client and initiates connections.
In order to use nc as a server, the command line -l (listen) must be specified.
The following command listens on port 80 and prints to stdout what it receives, then terminates:
$ sudo nc -l 80
In order to continously listen for new connections, the -k option needs to be specified:
$ sudo nc -lk 80
After a client has connected, the above command reads from stdin and returns the read text to the client.
With -d, nc does not attempt to read from stdin.

Simple webserver

It's possible, to write a simple web server (inspired by this stackoverflow answer).
cnt=0
while true; do
   let cnt++
   resp=$"<h1>Simple webserver</h1>My PID is <code>$$<</code>p>Request counter: <code>$cnt</code>"
   len="$(printf '%s' "$resp" | wc -c)"
   printf "HTTP/1.1 200 OK\r\nContent-Encoding: text/html\r\nContent-Length: $len\r\n\r\n<h1>Simple Webserver</h1>My PID is <code>$$</code><p>Request counter is <code>$cnt</code>"  | nc -Nl 8000
done
The -N option specifies that the socket is shutdown when it receives an EOF on the input. The man page says that «some servers require this to finish their work».
In an Oracle Linux environment, I found nc to be ncat (readlink -f /usr/bin/nc returned /usr/bin/ncat) which does not understand the -N option. In this environment, replacing -Nl8000 with -l 8000 --send-only did the job.
Also note that the request counter ($cnt) increases by two if the webserver is accessed with a traditional web browser because the browser also tries to fetch /favicon.ico.

Test client server

nc can be used to rudimentarily test a client-server scenario.
In a shell, the «server» is started using -l to indicate the port where the server is listening. -q 1 tells the server to quit if it doesn't receieve any input from a client within 1 second after delivering the message (here: «hello from server):
$ echo 'hello from server' | nc -l 1234 -q 1
After the server is started, a client is started in another shell that connects to port 1234, then prints the message received from the server and then delivers another message («this is the client») to the server:
$ echo 'this is the client' | nc localhost 1234 -q 1
hello from server
Going back the first shell, we see the server having printed the message from the client:
this is the client

Print a browser's HTTP request headers

nc  -kdl 8000

Slow HTTP Request

The following example sends the header of a HTTP request but then waits 2 minutes for the terminating empty line.
(echo -en "GET / HTTP/1.1\r\nHost: localhost\r\n"; sleep 120; echo -en "\r\n") | nc localhost 8080
See also the slowhttptest tool.

Binary alternatives

After installing netcat in Debian with apt, I found the following alternatives:
$ sudo apt install -y netcat
…
$ readlink /bin/netcat
/etc/alternatives/netcat

$ readlink /bin/nc
/etc/alternatives/nc

$ readlink /etc/alternatives/netcat
/bin/nc.openbsd

$ readlink /etc/alternatives/nc
/bin/nc.openbsd

See also

Shell commands

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Linux/sh...', 1759413236, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Linux/shell/commands/nc(139): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78