HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 50
ls = socket AF_INET, SOCK_STREAM, 0
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 50 highlights
Using Internet Stream Sockets Example Using Internet Stream Sockets * network at once will be able to have one server * listening on all networks at once. Even when the * host is connected to only one network, this is good * practice, because it makes the server program more * portable. */ 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", "tcp"); if (sp == NULL) { fprintf(stderr, "%s: host not found ", argv[0]); exit(1); } myaddr_in.sin_port = sp->s_port; /* Create the listen socket. */ ls = socket (AF_INET, SOCK_STREAM, 0); if (ls == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to create socket\n" , argv[0]); exit(1); } /* Bind the listen address to the socket. */ if (bind(ls, &myaddr_in, sizeof(struct sockaddr_in)) == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to bind address\n", argv[0]); exit(1); } /* Initiate the listen on the socket so remote users * can connect. The listen backlog is set to 5. 20 */ if (listen(ls, 5) == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to listen on socket\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]); 50 Chapter 2