Using socat to create a TCP Proxy
I wanted to set up a small TCP proxy so I could run a few tests, and I wasn't in the mood to run nginx or Envoy just for this. I stumbled upon socat (SOcket CAT) and found it pretty useful. Every time I discover a tool like this one that has been around for decades, I'm happy.
Here's how I used it:
socat TCP-LISTEN:5001,fork,reuseaddr TCP:127.0.0.1:5000
It listens on port 5001 and forwards incoming connections to 127.0.0.1:5000.
Here's what each option does:
TCP-LISTEN:5001Creates a TCP listening socket on port 5001. By default, it listens on all network interfaces (0.0.0.0). I was running this inside an incus container, so connections could arrive through the container's network IP.forkCreates a new process for each incoming connection. This allows multiple clients to connect simultaneously instead of handling only one connection at a time. This was useful for my test.reuseaddrSets the SO_REUSEADDR socket option, allowing the listening socket to reuse the address without waiting for certain previous socket states to expire. This was useful when restarting the proxy.TCP:127.0.0.1:5000Specifies the destination socket.
One important detail is that socat doesn't understand HTTP. It operates at the TCP level, forwarding bytes in both directions. This means it works with HTTP, HTTPS, and other TCP-based protocols without needing to know anything about them.
Written on 2026-11-28.