Reputation: 673
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?
Upvotes: 5
Views: 4248
Reputation: 9385
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. */
Upvotes: 7