Commands / Ports and connections

Ports and connections

socat

Also netcat++, unix socket

Connects any two data streams, network or local: a flexible nc, and the way to talk to a daemon's control socket.

socat A B copies data both ways between two addresses: a TCP or UDP port, a Unix socket, a file, your terminal (- or stdio). In the labs it runs small echo servers, and talks to HAProxy's control socket to enable or disable a server behind the balancer; the socat command examples below are the ones the labs run.

socat command usage

socat ADDRESS ADDRESS

Options

TCP-LISTEN:7,fork
listen on TCP 7, one child per connection
TCP:10.0.2.10:7
connect to TCP 7 on 10.0.2.10
UDP4:host:port,sourceport=5000
UDP to one peer, from a chosen port; replies from elsewhere are ignored
,reuseaddr
share the local port with another socket already bound to it
UDP4-DATAGRAM:host:port,bind=:5000
UDP to host:port from port 5000, taking datagrams from anyone
stdio / -
your terminal
/run/haproxy.sock
a Unix socket

socat examples

socat - TCP:10.0.2.10:7
Connects your terminal to TCP port 7 on 10.0.2.10.
socat TCP-LISTEN:7,fork EXEC:cat
Runs an echo server on TCP port 7 that answers every client.
echo 'show stat' | socat stdio /run/haproxy.sock
Asks HAProxy, through its control socket, for the state of every server.
socat - UDP4:10.0.4.10:3478,sourceport=5000
Sends each line you type as one UDP datagram from port 5000, and prints every answer.
echo alice bob | socat -t1 - UDP4:10.0.4.10:3478,sourceport=5000
Sends one datagram from port 5000 and prints the answer, waiting a second for it.

Pitfalls

  • Without ,fork a listener serves one client and exits.
  • A connected UDP socket (UDP4:) quits on the first ICMP port unreachable; UDP4-DATAGRAM: keeps going.

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Open this lab