简体   繁体   中英

How redirect stdout (stderr) of child process to socket in winapi?

I want to make client-server program where server is main process and the client is child process. In client I want to redirect stdout and stderr streams of child process to socket. In server I want to make from socket file descriptor and read from it. In internet there is lot of info about sockets, but I havn't found any example of parent-child IPC via sockets for windows.

If possible please post here a simple code which solves my problem (or part). Links to msdn also can help, but I think I've already looked up everyting there and not found what I want.

PS Please don't suggest pipes. I want to do this via sockets.

Take a look at Netcat if you can use this or find the source for the windows version somewhere on sourceforge probably this can use pipes to intercept stdin/stdout and send to and read from sockets with those pipes.

I know you said no pipes but there is no other way to do this with only windows API. You'll need the following at a minimum probably:

CreatePipe

WriteFile

ReadFile

This is like trying to put out a fire after its started, when you should be trying to prevent the fire in the first place.

Since you are the proclaimed author of both server.exe and client.exe, rather than trying to catch stdout/stderr, and redirecting it to a socket at that point, you should re-engineer the client so that you're catching the text destined for stdout, BEFORE it goes to stdout.

At that point, you can send the output text to BOTH stdout, and a socket if you wished.

EDIT: Ignore top half since you have later said you are NOT the author of client.

Ok, second try:

Since you are spawning the "real" client, using a "fake" client, why don't you just intercept stdout of "real" client on stdin of "fake" client (open a pipe). Then have "fake" client send data back on socket..

Assuming:

server talks with client

and

client spawns child

Here is an example from MSDN to create a Child Process with Redirected IO

Unfortunately, you MUST use pipes to get the IO from the child program to client. No ifs, ands or buts about it.

Then you can use something like the following to get the information to server:

client comms loop:

while ( /* files are open */ ) { 
    DWORD dwRead;  
    CHAR chBuf[1024]; memset(chBuf, 0, 1024); /* always initialize your memory! */
    BOOL bSuccess = FALSE;

    /* read data from child pipe */
    ReadFromFile(g_hSChildStd_IN_Rd, chBuf, 1024, &dwRead, NULL );      

    /* send data via windows sockets to the remote server connection 
       represented by serverSocketHandle */
    send(serverSocketHandle, chBuf, dwRead, 0);
}

Your client, in effect, becomes a translator between a STDOUT/ERR pipe and a TCP Socket.

The only environment I know that supports this is Cygwin. They are open source - try to peruse their code for ideas. It's guaranteed to be really nasty, though, with things like pumping threads all around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM