I'm looking for a tool that starts a socket server, and forwards incoming requests on that socket port to another internal one.

I found socat is able to forward socket ports in general:

socat tcp-l:8080,fork,reuseaddr tcp:

This forwards the "official" port 8080 to internal socket port 12345 successfully.

BUT: my problem is that clients should be able to connect to port 8080 even if internal port 12345 is currently unavailable.

The problem with socat is that it does not seem to create a real socket server, but justs remapps the port, and just puts the port on LISTENING.

I'm looking for a tool that tells the client "You connected successfully, please send me your input". And if my local port 12345 is not available, the clients should get an error when trying to send data.

So in case my local application is down, clients should still be able to establish a connection to that official ort.

Is that possible without having to write my own tool?

  • 1
    I'm not sure what you are asking. This command creates a listener on port 8080 as you require (it does no "remapping" - whatever this is). It will accept connections to port 8080 even if port 12345 is down as you require. Only, it will immediately close the socket if it realizes that it can not connect to the internal port 12345. I'm not sure what you expect here instead - since there is no application protocol defined there is no way to notify the client about the problem apart from closing the connection. Commented Jul 8, 2019 at 11:45
  • 1
    Ok I see. I wanted socat to not close the socket connection if the port could not be forwarded. But if that's not possible to prevent the socket closing, I'd probably have to write my own app for this task.... Commented Jul 8, 2019 at 11:57

2 Answers 2


You could start with:

socat tcp-l:8080,fork,reuseaddr \
SYSTEM:'nc -z 22 >/dev/null && exec nc 22 || echo "Fail!"'

So, Instead of tcp: you spawn a shell_fragment/script, that more-or-less:

Check, if 12345 responds
  if yes, connect there and pass over the fds (exec)
  if no, echo "Service@12345 is down"
    Optional: don't exit, but enter a while loop
      Try reaching port 12345, then connect eventually

For the Optional part, just replace 'echo "Fail!" from above with

{ echo "SomeMsg; MoreCommands; }

I can help, if you need help adapting this, just update your Q with info where you got stuck


ncat will get you where you need to be. It listens on a port and accepts packets. See for example:

 ncat -l localhost 8080
  • I don't think so. I mean, how does it forward the traffic to 12345 port? Commented Jul 9, 2019 at 7:47
  • I tried ncat -l localhost 8080 --sh-exec "ncat localhost 12345"` but that does not work so far. Commented Jul 9, 2019 at 7:54
  • I missed this part of the requirement. You may be able to use the -e or -c flag to accomplish this with a custom application as an intermediary. But frankly this is starting to seem like an XY problem. Commented Jul 9, 2019 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .