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?
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. */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With