Redirecting an established TCP connection to another process on another port?

On a Linux machine, you have a daemon that listens on TCP port A. However, it usually stops because it is rarely used and removes a large amount of system resources. Instead, I want to do something like this:

Encode the application that listens on port B and does the following as soon as the connection is established: if the daemon is stopped, start it and wait for it to listen on port A. Now the tricky part is to fully connect the client to the daemon, that is, without having to reconnect the client to port A. In addition, but this is not related to this issue, the application will disable the daemon if there are no connections for a while.

Of course, I could connect my application to the daemon and transfer all communications. I dont need it. I want to somehow redirect the established connection with the daemon, and then get rid of the connected socket, and the client is now happily connected to the daemon. In a way, I want to give the daemon process its socket that is already connected. Is there a way to do something like this?

I am running Debian if this is important. I would like to encode the application in C / C ++, and everything is fine to have OS-specific solutions (i.e. use system calls). Forgive me, although I am not very similar to a Linux encoder, so I am not very familiar with Linux system programming. If there is any obvious way to do this, I simply did not know.

Of course, I am open to any suggestion.

+4
source share
1 answer

This problem has an existing standard solution, usually called inetd . It has been around for a long time, first on Unix systems and then on Linux.

More modern implementation of xinetd

+2
source

Source: https://habr.com/ru/post/1499663/


All Articles