将 stdout 和 stderr 重定向到分布式 shell 程序的套接字
我制作了一个具有客户端和服务器的分布式 shell 程序。客户端向服务器发送命令请求,服务器在本地执行该命令,并将该命令的结果输出给客户端。我无法弄清楚如何将 stdout/stderr 重定向到客户端。我使用 execvp 来执行命令。
我想我可能必须使用 dup2 ?但我不知道如何正确使用它。有什么帮助吗?
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.
I think I might have to use dup2? But I can't figure out how to use it properly. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需使用 dup2() 将套接字的文件描述符复制到 stderr 和 stdout 文件描述符上。这与重定向到管道几乎是一样的。
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.