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

发布于 2024-10-24 08:44:40 字数 278 浏览 1 评论 0原文

我想制作客户端-服务器程序,其中服务器是主进程,客户端是子进程。在客户端中,我想将子进程的 stdout 和 stderr 流重定向到套接字。在服务器中,我想从套接字文件描述符创建并从中读取。互联网上有很多关于套接字的信息,但我还没有找到任何通过 Windows 套接字进行父子 IPC 的示例。

如果可能,请在此处发布一个简单的代码来解决我的问题(或部分问题)。 msdn 的链接也可以提供帮助,但我想我已经在那里查找了所有内容,但没有找到我想要的内容。

PS 请不要建议管道。我想通过套接字来做到这一点。

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫尐诺 2024-10-31 08:44:40

看看Netcat如果你可以使用这个或者在sourceforge上找到Windows版本的源代码可能是这个可以使用管道拦截 stdin/stdout 并使用这些管道发送到套接字并从套接字读取。

我知道你说没有管道,但没有其他方法可以仅使用 Windows API 来做到这一点。您可能至少需要以下内容:

CreatePipe

WriteFile

< a href="http://msdn.microsoft.com/en-us/library/aa365467%28v=vs.85%29.aspx" rel="nofollow">阅读文件

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

Oo萌小芽oO 2024-10-31 08:44:40

这就像火灾发生后试图将其扑灭,而您应该首先努力防止火灾发生。

由于您是 server.exe 和 client.exe 的声明作者,因此您应该重新设计客户端以便捕获文本,而不是尝试捕获 stdout/stderr 并将其重定向到套接字目的地是标准输出,然后再发送到标准输出。

此时,您可以将输出文本发送到标准输出和套接字(如果您愿意)。

编辑:忽略上半部分,因为您后来说过您不是客户端的作者。

好吧,第二次尝试:

既然您正在使用“假”客户端生成“真实”客户端,为什么不'您只是在“假”客户端的标准输入上拦截“真实”客户端的标准输出(打开管道)。然后让“假”客户端在套接字上发送回数据。

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..

小傻瓜 2024-10-31 08:44:40

据我所知,唯一支持此功能的环境是 Cygwin。它们是开源的 - 尝试仔细阅读他们的代码以获取想法。不过,像到处泵送线程这样的事情肯定会非常令人讨厌。

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.

诗酒趁年少 2024-10-31 08:44:40

假设:

server talks with client

client spawns child

是 MSDN 中创建 带有重定向 IO 的子进程

不幸的是,您必须使用管道将 IO 从子程序获取到客户端。没有如果、和或但是。

然后,您可以使用类似以下内容的方法将信息发送到服务器:

客户端通信循环:

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);
}

实际上,您的客户端成为 STDOUT/ERR 管道和 TCP 套接字之间的转换器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文