Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using socat to multiplex incoming tcp connection

Tags:

socat

An external data provider makes a tcp connection to one of our servers.

I would like to use socat to 'multiplex' the incoming data so that multiple programs can receive data sent from the external data provider.

socat -u TCP4-LISTEN:42000,reuseaddr,fork OPEN:/home/me/my.log,creat,append

happily accepts incoming data and puts it into a file.

What I'd like to do is something that will allow local programs to connect to a TCP port and begin to receive data that arrives from connections to the external port. I tried

socat -u TCP4-LISTEN:42000,reuseaddr,fork TCP4-LISTEN:43000,reuseaddr 

but that doesn't work. I haven't been able to find any examples in the socat doco that seem relevant to back to back TCP servers.

Can someone point me in the right direction?

like image 239
RoyHB Avatar asked Jul 05 '13 04:07

RoyHB


People also ask

What is Socat tool used for?

Socat allows for bidirectional data transfers from one location to another. The socat utility is a relay for bidirectional data transfers between two independent data channels.

Can you connect to Socat with netcat?

Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party.

What does fork do in Socat?

The fork option just makes it fork a new child to process the newly accepted connection while the parent goes back to waiting for new connections.

What is Socat command in Linux?

Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.


1 Answers

With Bash process-substitution

Multiplexing from the shell can in general be achieved with coreutils tee and Bash process-substitution. So for example to have the socat-stream multiplexed to multiple pipelines do something like this:

socat -u tcp-l:42000,fork,reuseaddr system:'bash -c \"tee >(sed s/foo/bar/ > a) >(cat > b) > /dev/null\"'

Now if you send foobar to the server:

socat - tcp:localhost:42000 <<<fobar

Files a and b will contain:

a

barbar

b

foobar

With named pipes

If the pipelines are complicated and/or you want to avoid using Bash, you can use named pipes to improve readability and portability:

mkfifo x y

Create the reader processes:

sed s/foo/bar/ x > a &
cat y > b &

Start the server:

socat -u tcp-l:42000,fork,reuseaddr system:'tee x y > /dev/null'

Again, send foobar to the server:

echo foobar |  socat - tcp:localhost:42000

And the result is the same as in the above.

like image 163
Thor Avatar answered Sep 22 '22 12:09

Thor