Re: client server communication and system() call



phil-news-nospam@xxxxxxxx writes:
On Tue, 20 May 2008 10:08:45 +0500 arnuld <sunrise@xxxxxxxxxxxxxxxx> wrote:
|> On Mon, 19 May 2008 12:52:11 +0200, Rainer Weikusat wrote:
|
|> Just in case this isn't clear without it: The dup2-call can be used to
|> cause an existing descriptor to refer to the file description
|> associated with another descriptor. In this particular case, it would
|> likely look like
|>
|> dup2(accepted_fd, STDOUT_FILENO)
|>
|> executed in a forked process. This means that another process started
|> from the one where the redirection is executed will use the
|> established TCP-connection whenever it prints anything on its
|> 'standard output channel'.
|
|
| it works fine :)
|
| dup2( accept_fd, 1 );
| system( store_in );
|
| but Googling also gives this alternative where the poster claims that
| use of dup2 is reduant and I should use this one:
|
| close(1);
| dup( accept_fd );
| system( store_in );
|
|
| what say ?

That depends on STDIN being open. In most all cases it probably is.
Otherwise you could find that dup() puts the new descriptor at 0 and
you don't get any of the output. So go with the dup2() solution.


Even worse, in a multithreaded program, there is no guarantee that
another thread didn't do an open between the close and the dup. Always
use dup2.

scott
.



Relevant Pages