Home > OS >  Linux dup and dup2 what was the real application scenario?
Linux dup and dup2 what was the real application scenario?

Time:04-13

Recently in "Linux high-performance server programming", the book has a piece of code is written in the dup function with a simple CGI servers, the code is like this:
 
#include
#include
#include
#include
#include
#include
#include
#include
#include

Int main (int arg c, char * argv [])
{
If (argc
=2){
Usage: printf (" % s ip_address port_number \ n ", the basename (argv [0]));
return 1;
}
IP=const char * argv [1].
Int port=atoi (argv [2]).

Struct sockaddr_in address;
Bzero (& amp; Address, sizeof (address));
Address. Sin_family=AF_INET;
Inet_pton (AF_INET, IP, & amp; Address. Sin_addr);
Address. Sin_port=htons (port);

Int the sock=socket (PF_INET SOCK_STREAM, 0).
Assert (sock>=0);

Int ret=bind (sock, (struct sockaddr *) & amp; Address, sizeof (address));
Assert (ret!=1);

Ret=listen (sock, 5);
Assert (ret!=1);

Struct sockaddr_in client;
Socklen_t client_addrlength=sizeof (the client);
Int connfd=accept (the sock, (struct sockaddr *) & amp; Client, & amp; Client_addrlength);
If (connfd<0)
{
Printf (" errno is: % d \ n ", errno);
}
The else
{
Close (STDOUT_FILENO);
Dup (connfd);
Printf (" abcd \ n ");
Close (connfd);
}

Close (sock);
return 0;
}


Read my own feeling and ordinary TCP communications each difference, so I doubt dup really is used in the function in the network programming? In other places use and use in port redirection symbols what's the difference?
  • Related