Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect stdout and stderr to socket for a distributed shell program

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?

like image 422
y3di Avatar asked Nov 10 '11 18:11

y3di


1 Answers

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. */
like image 164
Dmitri Avatar answered Sep 18 '22 05:09

Dmitri