HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 51

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

fprintf(stderr, "%s: unable to fork daemon\n", argv[0]);
exit(1);
case 0:
/* The child process (daemon) comes here. */
/* Close stdin and stderr so that they will not
* be kept open.
* redirected to some logging file, or /dev/null.
* From now on, the daemon will not report any
* error messages.
* waiting for connections and forking a child
* server to handle each one.
*/
fclose(stdin);
fclose(stderr);
/* Set SIGCLD to SIG_IGN, in order to prevent
* the accumulation of zombies as each child
* terminates.
* have to make wait calls to clean them up.
*/
signal(SIGCLD, SIG_IGN);
for(;;) {
/* Note that addrlen is passed as a pointer
* so that the accept call can return the
* size of the returned address.
*/
addrlen = sizeof(struct sockaddr_in);
s = accept(ls, &peeraddr_in, &addrlen);
if ( s == -1) exit(1);
switch (fork()) {
case -1:
case 0:
default:
}
}
default:
exit(0);
}
Chapter 2
Example Using Internet Stream Sockets
Stdout is assumed to have been
This daemon will loop forever,
This means the daemon does not
/* This call will block until a new
* connection arrives. Then, it will
* return the address of the connecting
* peer, and a new socket descriptor,
* s, for that connection.
*/
/* Can't fork, just continue. */
exit(1);
/* Child process comes here. */
server();
exit(0);
/* Daemon process comes here. */
/* The daemon needs to close the
* the new accept socket after
* forking the child. This
* prevents daemon from running
* out of file descriptors.
* It also means that when the
* server closes the socket,
* that it will allow socket
* to be destroyed since it
* will be the last close.
*/
close(s);
/* Parent process comes here. */
Using Internet Stream Sockets
51

Advertisement

Table of Contents
loading

Table of Contents