问题
I am running a client program that communicates with a server using TCP sockets. When I start the client it immediately creates a socket connection with the server and does not close the connection.
What I am wondering is, can I create another application (independent of the client/server apps) that sends data on this already open socket?
I know of a program that seems to do exactly this. The program is designed to sniff packets between the client and server and also allow you to send data on the connection. Before data can be sent you must provide the program with the client process name (eg. client.exe). I believe what it's doing is using the client process as a sort of proxy to the server (which is not what I want to do, I want to be able to connect directly to the server).
回答1:
Yes, you can, udner specific circumstances - provided your 2nd app runs on the same host as the client and that is a Unix OS.
What you need to do is have your client application and 2nd app open a Unix domain socket between them and then get the client application to pass to the 2nd application a copy of the open file descriptor it has obtained for the socket.
See this SO question foe details on how to do that: Portable way to pass file descriptor between different processes
来源:https://stackoverflow.com/questions/11876083/using-a-third-party-application-to-send-data-to-an-already-open-tcp-connection