0

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?

oddy
  • 1,890
  • 2
  • 16
  • 26
  • The address is already in use. Try another port. – user207421 Nov 18 '13 at 09:17
  • @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 Nov 18 '13 at 10:06
  • 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 Nov 18 '13 at 22:39
  • @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 Nov 19 '13 at 00:07
  • 1
    You are trying to bind two sockets to the same port. Somewhere. Don't. It doesn't work. – user207421 Nov 19 '13 at 09:27
  • @EJP Good day, sir. I'm afraid, I have issues with what to do, and not with what to avoid of doing. But thank you for your sincere advice. – oddy Nov 19 '13 at 10:03
  • 'What to do' is to remove one of the binds. You can't have both `socat` and your own software listening to the same port. – user207421 Nov 20 '13 at 03:04
  • @EJP Unfortunately for me, that would mean that you do not know how to help in my situation. Because removing something per se would not help me to accomplish what I want. Thank you for your effort. – oddy Nov 20 '13 at 09:23
  • You can't accomplish whatever it is you want to do by attempting operations which you have already discovered to be impossible. Nor by simply ignoring what you're being told here. I suggest you make an effort to distinguish between your problem and your attempted solution. At the moment this has all the hallmarks of an XY problem. – user207421 Nov 21 '13 at 10:55
  • What you want is simply not possible. 1 server = 1 port. 2 servers = 2 ports. You can connect any number of clients to one port, but you can only bind one server process. – Damon Nov 21 '13 at 13:08
  • @Damon My task was not to bind two sockets, but to make communication between software and device possible. Roland Bar below gave an answer that I was looking for. Thank you. – oddy Nov 22 '13 at 09:59
  • @EJP As I said before, I know that my attempted solution is wrong. If I knew what was wrong, I wouldn't ask. Fortunately, answer was found here. It was constructive and to the point. – oddy Nov 26 '13 at 21:26

2 Answers2

4

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
Luqman
  • 1,348
  • 1
  • 9
  • 6
Roland Bär
  • 1,720
  • 3
  • 22
  • 33
  • This looks very promising and sounds clear to me. I'll try that later today. – oddy Nov 21 '13 at 21:36
  • Man, that worked great, you're awesome! I'll edit my question so that it should be more clear for everyone what I needed. I need to wait another hour before SO will allow me to award your answer. – oddy Nov 22 '13 at 08:33
1

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?

Community
  • 1
  • 1
Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51