重定向输出和错误到插座分布式shell程序 [英] Redirect stdout and stderr to socket for a distributed shell program
问题描述
我由具有客户机和服务器的分布式外壳程序。客户端发送一个命令请求到服务器和服务器本地执行该命令,并应该输出该命令给客户的结果。我有麻烦搞清楚如何标准输出/标准错误重定向到客户端。我用execvp执行的命令。
I made a distributed shell program that has a client and server. The client sends a command request to the server and the server executes that command locally and is supposed to output the results of that command to the client. I am having trouble figuring out how to redirect stdout/stderr to the client. I use execvp to execute the command.
我想我可能需要使用dup2?但我无法弄清楚如何正确使用它。任何帮助吗?
I think I might have to use dup2? But I can't figure out how to use it properly. Any help?
推荐答案
您只需要使用 dup2()
复制套接字的文件描述符到标准错误和标准输出文件描述符。这是pretty多重定向到管道同样的事情。
You just need to use dup2()
to duplicate the socket's file descriptor onto the stderr and stdout file descriptors. It's pretty much the same thing as redirecting to pipes.
cpid = fork();
if (cpid == 0) {
dup2(sockfd, STDOUT_FILENO);
dup2(sockfd, STDERR_FILENO);
execvp(...);
/*... etc. etc. */
这篇关于重定向输出和错误到插座分布式shell程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!