Search code examples
linuxsocketstcpserial-portsocat

bi-directional socket to tcp communication


I have a software X listening and writing to TCP port. I.e. It creates a Server Socket and a client that reads and writes to TCP.

And I have a serial device ttyUSB0 which can accept data in a format that provides software X and send data back to serial.

I want to feed data from serial to TCP and vice versa, so that it looked transparent to software X and to a serial ttyUSB0.

I was trying to use socat. Like,

 socat -d -d -d -d -x TCP-LISTEN:7758,fork,reuseaddr FILE:/dev/ttyUSB0,b9600,raw

But it seems that it does not work. Looks as if listener on TCP port blocks binding. I have

E bind(3, {AF=2 0.0.0.0:7758}, 16): Address already in use

Could someone please help me with my problem?


Solution

  • As some commenters already mentioned, you can't make a TCP connection with two listeners. For a TCP connection you always need a server (listener) and a client.

    As your software is already a server (listening on port 7758) socat should be run in client mode (connecting to your server).

    This can be done with the option TCP:<host>:<port>, for example like this (adapted your example, not tested!):

    socat -d -d -d -d -x TCP:localhost:7758 FILE:/dev/ttyUSB0,b9600,raw