0
votes

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?

2
The address is already in use. Try another port.user207421
@EJP I would though I have described my problem quite clear. I'll make it simpler: Software A listens and writes to port x. Software B listens and writes to serial y. I tried to bind x <-> y using socat. I know that A occupies x. That knowledge does not help me solve my problem.oddy
But why are you trying to bind two sockets to the same port? It doesn't make sense. Bind the listening socket, then connect the other socket to that port.user207421
@EJP My purpose is in description. My implementation most probably is incorrect. But I am not following your point either. First of all, I have 3 listening sockets. Which one are you referring to? bi-directional mapping means both sockets listen and write. And I have socat as a broker: A <-> x <-> socat <-> y <-> B So, what do you suggest me to do then, given A(x), B(y) and socat?oddy
You are trying to bind two sockets to the same port. Somewhere. Don't. It doesn't work.user207421

2 Answers

4
votes

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
1
votes

This is not possible for TCP. Note that you could specify SO_REUSSEADDR but this will not cause BOTH listening applications to receive the data: only one app (decided at "random" by the OS) will receive the data, while the other will "hear" nothing.

If you can use multicast UDP you can do this.

See Can two applications listen to the same port?