Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6804931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:31:34+00:00 2026-05-26T19:31:34+00:00

I am trying to redirect STDOUT AND STDERR to a socket. I did: if(fork()

  • 0

I am trying to redirect STDOUT AND STDERR to a socket.

I did:

if(fork() == 0)
{
   dup2(newsock, STDOUT_FILENO);
   dup2(newsock, STDERR_FILENO);
   execvp();
}

Somehow, it only showed the first little part of the output.

for example, it showed on “mkdir” when I try to execute ls or mkdir.

What’s the problem?

I tried the flollowing it works, but I can only redirect one of STDOUT or STDERR

close(1);
dup(newsock);

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:31:35+00:00Added an answer on May 26, 2026 at 7:31 pm

    Your use of dup2() looks fine, so the problem is probably elsewhere. The simple program I threw together to test with does not have the issues you are experiencing, so I’ll just go over the core of it (around the fork()/execvp() area) with some error checking omitted for brevity:

    int    lsock, /* listening socket */
           csock; /* active connection's socket */
    pid_t  cpid;  /* child process ID from fork() */
    char   *cmd = "somecommand";
    char   *cmd_args[] = { "somecommand",
                           "firstarg",
                           "secondarg",
                           "howevermanyargs",
                           NULL }; /* note: last item is NULL */
    /*  ... 
        call socket(), bind(), listen(), etc.
        ... */
    
    for (;;) {  /* loop, accepting connections */
      if ( (csock = accept( lsock, NULL, NULL )) == -1) exit(1);
      cpid = fork();
      if (cpid < 0) exit(1);  /* exit if fork() fails */
      if ( cpid ) {
        /* In the parent process: */
        close( csock ); /* csock is not needed in the parent after the fork */
        waitpid( cpid, NULL, 0 ); /* wait for and reap child process */
      } else {
        /* In the child process: */
        dup2( csock, STDOUT_FILENO );  /* duplicate socket on stdout */
        dup2( csock, STDERR_FILENO );  /* duplicate socket on stderr too */
        close( csock );  /* can close the original after it's duplicated */
        execvp( cmd, cmd_args );   /* execvp() the command */
      }
    }
    

    The above is the core of a very basic server (only one client at a time) that, when it receives a connection, forks a new process to run a command and sends its stderr and stdout to the client over the socket. Hopefully you can solve your problem by examining it — but don’t just copy the code without understanding what it does.

    Try testing by connecting with a telnet client first… if it works with telnet but not with your client program, then look for problems in your client program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to redirect output of stdout and stderr using a file. I
I'm trying to redirect all output (stdout + stderr) of a Windows command to
I'm trying to redirect stdin and stdout of a console application, so that I
On Linux, I'm trying to redirect stdout from a console application to a file
I want a script to redirect stdout and stderr to a file, do a
I'm trying to redirect the output from manage.py to a text file, but only
I'm trying to redirect stdout to a file and then restore it back to
We've been trying to redirect from one action to another, hoping that data would
I am trying to redirect pages using mod_rewrite to the pages with some variables
I am trying to redirect ALL requests for mydomain.com whether they are something like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.