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

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

Advertisement

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.
* 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 ",
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.
*/
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.
* 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]);
50
argv[0]);
The listen backlog is set to 5. 20
Now we can fork the daemon and
We need to do a setpgrp
Otherwise, if the child
Even when the
This is done
It is
Chapter 2

Advertisement

Table of Contents
loading

Table of Contents