On a Linux machine, you have a daemon that listens on TCP port A. However, it is usually stopped because it is rarely used and takes away a large amount of system resources. Instead, I want to do something like this:
在Linux机器上,您有一个在TCP端口a上监听的守护进程。但是,它通常会被停止,因为它很少被使用,并且会占用大量的系统资源。相反,我想做这样的事情:
Code an application that listens on port B and does the following as soon as a connection is established: If the daemon is stopped, start it and wait until it listens on port A. Now the difficult part: Connect the client to the daemon in a completely transparent way, i.e. without the client having to reconnect on port A. Also, but this is irrelevant for this question, the application will shut down the daemon when there are no connections for a certain amount of time.
代码的应用程序侦听端口B,并做以下一旦建立连接:如果守护进程停止,启动它,等待它监听端口a现在困难的部分:连接客户端守护进程在一个完全透明的方式,即客户端无需重新连接在端口a,但这对于这个问题是无关紧要的,应用程序将关闭守护进程在没有连接了一定量的时间。
Of course, I could have my application connect to the daemon and pipe all communication. I do not want that. I want some way to forward the established connection to the daemon and then get rid of the connected socket, while the client is now happily connected with the daemon. In some way, I want to give the daemon's process my already connected socket. Is there any way to do something like this?
当然,我可以让我的应用程序连接到守护进程和管道所有通信。我不想那样。我希望有某种方法将已建立的连接转发到守护进程,然后删除已连接的套接字,而客户端现在正与守护进程愉快地连接。在某种程度上,我希望为守护进程提供已经连接的套接字。有什么办法可以这样做吗?
I'm running Debian, if that's important. I would want to code the application in C/C++, and it's okay to have OS-specific solutions (i.e. use syscalls). Forgive me though, I am not much of a Linux coder, so I am not very familiar with Linux system programming. If there is some obvious way to do it, I simply didn't know.
我在运行Debian,如果这很重要的话。我想用C/ c++编写应用程序,可以使用特定于操作系统的解决方案(例如使用syscalls)。请原谅,我不是一个Linux程序员,所以我不太熟悉Linux系统编程。如果有什么明显的方法,我也不知道。
Of course, I am open for any kind of suggestion.
当然,我愿意接受任何建议。