Linux – socat forwarding to multiple addresses

linuxmultiplexingsocat

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.

Best Answer

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.