6

I have a single incoming video stream, and I'd like to set up multiple processes to handle it, each running at its own address. After some reading it seems socat is one way to do this

socat TCP4-LISTEN:1934,fork,reuseaddr TCP4:someaddress:1935 

This forwards everything to the target address, but it lets me bind one target only. Is there a way to bind multiple target addresses with socat? If I try multiple individual binds I get an "address already in use" error on subsequent binds. I'm not forced to use socat, anything that lets me copy my incoming stream is good, if anyone knows a better approach. Thanks.

Shukri Adams
  • 163
  • 1
  • 1
  • 6
  • I'm scratching my head over this one. It should work. Does `netstat -nl -A inet` show a listener on port 1934? – Andrew Schulman Feb 02 '18 at 17:02
  • ... I mean after the first client connects, and you want to connect again. – Andrew Schulman Feb 02 '18 at 17:10
  • Have a look at https://gist.github.com/mathieue/3505472 taken from a comment into a duplicate question: https://stackoverflow.com/questions/18057619/using-socat-to-relay-one-tty-stream-to-multiple-tcp-ip-destinations-plus-to-one – Patrick Mevzek Feb 02 '18 at 17:20
  • @AndrewSchulman Yes, it shows a listener on that part. – Shukri Adams Feb 04 '18 at 22:02
  • @PatrickMevzek that gist is for UDP ... how would I do that for TCP? – Shukri Adams Feb 04 '18 at 22:13
  • Just replace `udp4-listen` by `tcp4-listen` and the two `udp-sento` by `tcp4` ? See answers of this question too: https://stackoverflow.com/questions/17480967/using-socat-to-multiplex-incoming-tcp-connection – Patrick Mevzek Feb 04 '18 at 22:27
  • @PatrickMevzek Tried socat - tcp4-listen:1934,fork | tee >(socat - tcp4:127.0.0.1:1935) >(socat - tcp4:127.0.0.1:1936) I can see something is listening on 1935, but it just consoles out a bunch of garbled characters when I try to stream to it, and nothing after that. – Shukri Adams Feb 04 '18 at 23:42
  • `socat -u` instead? – Patrick Mevzek Feb 05 '18 at 00:45

1 Answers1

5

Based on my earlier comments, I have just tested this solution[1] and it works as expected, if I understood your needs:

socat -u tcp4-listen:1934 - | tee >(socat - tcp4:127.0.0.1:1935) >(socat - tcp4:127.0.0.1:1936) > /dev/null

There must be processes already listening on 1935 and 1936 already before launching this otherwise the socat on the right will complain.

This is still a very brittle solution. I would advise trying alternative routes, like a temporary caching file or a pipe maybe.

[1] like that:

  • nc -l 127.0.0.1 1935 in a shell
  • nc -l 127.0.0.1 1936 in another shell
  • the command above in the third shell
  • and then in a fourth one: echo "foobar" | nc 127.0.0.1 1934
  • and I do see "foobar" in output in first and second shell.
Patrick Mevzek
  • 9,273
  • 7
  • 29
  • 42
  • Thanks for the help, I'm accepting this as it does answer my original question. But as you said, this whole thing feels brittle, and I think I need to come at this problem from a whole other angle. – Shukri Adams Feb 08 '18 at 10:33