HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 103
s = socket AF_INET, SOCK_DGRAM, 0
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 103 highlights
Using Internet Datagram Sockets Example Using Datagram Sockets */ 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", argv[0]); 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. Now we can fork the daemon and * return to the user. We need to do a setpgrp * so that the daemon will no longer be associated * with the user's control terminal. This is done * before the fork, so that the child will not be * a process group leader. Otherwise, if the child * were to open a terminal, it would become associated * with that terminal as its control terminal. It is * 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. From now on, the daemon will * not report any error messages. This daemon * will loop forever, waiting for requests and * responding to them. */ fclose(stdin); fclose(stdout); fclose(stderr); Chapter 4 103