Socat port forwarding

socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. It has lots of feauters you can see on it’s man page.

Here we discuss a general problem: You just changed your ISP and your website has new ip address. But, some requests still comes to old ip due to the DNS cache issues. So, you want to forward all traffic which comes to old ip to new ip transparently for a few days like port forwarding in a gateway:

socat TCP-LISTEN:80,fork TCP:NEW_IP:80

In this example, socat waits for a connection to port 80 on old ip server and accepts new connections, forks and transparently relays to new host’s port 80.

Socat can forward UDP sockets too. Indeed, it is even possible to listen UNIX domain socket with socat and forward it to a remote TCP socket. Thanks for this great utility.