for (;;)
{ /* loop, accepting connections */
if ( (csock = accept( serverSockfd, 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: */
printf("execvp\n");
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 */
char *cmd = "/usr/bin/man";
char *cmdArgs[] = { "man",
"unix",
NULL }; //note: last item is NULL
/*
char *cmd = "ls";
char *cmdArgs[] = { "ls",
"/home/user",
NULL }; //note: last item is NULL
*/
execvp( cmd, cmdArgs ); /* execvp() the command */
}
}