*/
myaddr_in.sin_addr.s_addr = INADDR_ANY;
/* Find the information for the "example" server
* in order to get the needed port number.
*/
sp = getservbyname ("example", "udp");
if (sp == NULL) {
printf("%s: host not found",
exit(1);
}
myaddr_in.sin_port = sp->s_port;
/* Create the socket. */
s = socket (AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
perror(argv[0]);
printf("%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Bind the server's address to the socket. */
if (bind(s, &myaddr_in, sizeof(struct sockaddr_in)) == -1) {
perror(argv[0]);
printf("%s: unable to bind address\n", argv[0]);
exit(1);
}
/* Now, all the initialization of the server is
* complete, and any user errors will have already
* been detected.
* return to the user.
* so that the daemon will no longer be associated
* with the user's control terminal.
* before the fork, so that the child will not be
* a process group leader.
* were to open a terminal, it would become associated
* with that terminal as its control terminal.
* always best for the parent to do the setpgrp.
*/
setpgrp();
switch (fork()) {
case -1: /* Unable to fork, for some reason. */
perror(argv[0]);
printf("%s: unable to fork daemon\n", argv[0]);
exit(1);
case 0:
/* The child process (daemon) comes here. */
/* Close stdin, stdout, and stderr so they will
* not be kept open.
* not report any error messages.
* will loop forever, waiting for requests and
* responding to them.
*/
fclose(stdin);
fclose(stdout);
fclose(stderr);
Chapter 4
argv[0]);
Now we can fork the daemon and
We need to do a setpgrp
Otherwise, if the child
From now on, the daemon will
This daemon
Using Internet Datagram Sockets
Example Using Datagram Sockets
This is done
It is
103