简体   繁体   English

如何将子进程的标准输出(stderr)重定向到winapi中的套接字?

[英]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. 在客户端中,我想将子进程的stdout和stderr流重定向到套接字。 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. 在Internet上有很多有关套接字的信息,但是我还没有通过Windows套接字找到任何父子IPC的示例。

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. 链接到msdn也可以提供帮助,但是我想我已经在这里查找了所有内容,但没有找到我想要的。

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. 看一下Netcat,如果可以使用它,或者在sourceforge上的某处找到Windows版本的源,可能可以使用管道来拦截stdin / stdout并使用这些管道向套接字发送和读取套接字。

I know you said no pipes but there is no other way to do this with only windows API. 我知道您说过没有管道,但是只有Windows API才能做到这一点。 You'll need the following at a minimum probably: 您可能至少需要以下内容:

CreatePipe CreatePipe

WriteFile WriteFile的

ReadFile 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. 由于您是server.exe和client.exe的知名作者,而不是试图捕获stdout / stderr并在那时将其重定向到套接字,因此您应该重新设计客户端,以便捕获文本。注定要用于stdout,然后再转到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). 由于使用“伪”客户端生成“真实”客户端,所以为什么不截取“伪”客户端的stdin上的“真实”客户端的标准输出(打开管道)。 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 这是MSDN中使用重定向IO创建子进程的示例

Unfortunately, you MUST use pipes to get the IO from the child program to client. 不幸的是,您必须使用管道将IO从子程序获取到客户端。 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. 实际上,您的客户端将成为STDOUT / ERR管道和TCP套接字之间的转换器。

The only environment I know that supports this is Cygwin. 我知道支持此功能的唯一环境是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. 但是,它保证真是令人讨厌的,诸如到处都是泵送线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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