The goal: bidirectional communication while decode in a unidirectional way the incoming data

Theory: suppose to have a proxy/ server that listens on port 8080 which needs to handle multiple clients at once. The data coming in is base64 encoded, it should be decoded and forwarded to another port ( 80 ).

Practice: I would like to use socat because of all its features ( and also because it is the only one I know that can do all these things, if you know something else could be fine too ):

  • tcp to tcp
  • udp to tcp
  • fork and reuseaddr
  • exec commands

I'm looking for something like that:

socat tcp-listen:8080,reuseaddr,fork exec:/path/to/myscript.sh

with /path/to/myscript.sh:

exec base64 -d | socat - tcp:localhost:80

But this sadly does not fit my case because is not completely bidirectional

  • 1
    Does it work better if you replace exec base64 -d with stdbuf -o0 recode /b64? Jul 27, 2021 at 15:45

1 Answer 1


Your main problem is probably that base64 -d (GNU base64 for me) buffers its output, and by the look of it, can't be told not to. Same goes for openssl base64 -d from my tests. GNU recode can be told not to buffer with GNU stdbuf though.

So with socat 2, you should be able to do something like:

socat tcp-listen:8080,fork,reuseaddr \
      'exec1:stdbuf -o0 recode /b64 % nop | tcp:localhost:80'

With socat 1, you could do:

socat tcp-listen:8080,fork,reuseaddr \
      'system:"stdbuf -o0 recode /b64 | socat - tcp:localhost:80"'

Which is essentially the same as yours but with the intermediate script inlined (and the non-buffering base64 decoding).

  • I installed recode and I tried what you suggest, but it gives me an error sadly. The command I used: socat tcp-listen:8080,fork,reuseaddr 'exec:stdbuf -o0 recode /b64 % nop | tcp\:localhost\:80'. The error: recode: fopen (%): No such file or directory. By the way, thanks for your help! (:
    – hyogy
    Jul 27, 2021 at 17:17
  • You'd need socat 2 for that address chaining capability. Jul 27, 2021 at 17:18
  • @hyogy, see edit for socat 1. Jul 27, 2021 at 17:22
  • AAH the second version of socat! I thought you mistyped the 2! ahah sorry! I tried your edit, it works!!! Thanks!!!
    – hyogy
    Jul 27, 2021 at 17:26
  • 1
    @hyogy, I suspect it's just 'system:"stdbuf -o0 tr... | socat - tcp:localhost:80 | stdbuf -oO tr..."'. But I don't have time to look into it atm. Feel free to answer your own question if that works. Aug 19, 2021 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.