network-programmingportmodbusnetcatbridge

Bridging two ports with netcat/socat


I need to get data from a ModBus device (modbus slave) over TCP but this device has to be exposed as a TCP client (it eats much less from the battery in this case). It means that both machines have to connect to the third one as TCP clients an I have to build a bridge between two ports, something like this

[modbus slave] -> [4444:bridge:5555] <- [modbus master]

I did try it with the netcat on a bridge machine

$ /bin/netcat -lk 5555 | /bin/netcat -lk 4444

It works the half way: I can connect to 4444 with my slave and to 5555 with my master, and traffic flows from master to slave. However, I do not see any traffic in the opposite direction. How do I build a two-way bridge in this case?

Thanks a lot in advance!


Solution

  • You can have the connections automatically recover after either end disconnects (so that the clients can reconnect immediately). Do this by setting -k flag. Also note, that you probably want to limit the number of connections to 1 on either side using -m parameter. Otherwise, you can bridge thousands of clients together! So putting it all together:

    # mkfifo fifo
    # nc -lkm1 -p4444 < fifo | nc -lkm1 -p5555 > fifo