Modifiche varie e ripulitura esempi. Trattata accept4.
[gapil.git] / listati / TCP_echod_first_init.c
1 int main(int argc, char *argv[])
2 {
3     int list_fd, conn_fd; 
4     pid_t pid;
5     struct sockaddr_in serv_add;
6     ...
7     /* create and init socket */
8     if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
9         perror("Socket creation error");
10         exit(1);
11     }
12     memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */
13     serv_add.sin_family = AF_INET;                  /* address type is INET */
14     serv_add.sin_port = htons(7);                   /* echo port is 7 */
15     serv_add.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
16     if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
17         perror("bind error");
18         exit(1);
19     }
20     /* give away privileges and go daemon */
21     if (setgid(65534) !=0) { /* first give away group privileges */
22         perror("cannot give away group privileges");
23         exit(1);
24     }
25     if (setuid(65534) !=0) { /* and only after user ... */
26         perror("cannot give away user privileges");
27         exit(1);
28     }
29     if (demonize) {          /* go daemon */
30         openlog(argv[0], 0, LOG_DAEMON); /* open logging */
31         if (daemon(0, 0) != 0) {
32             perror("cannot start as daemon");
33             exit(1);
34         }
35     }
36     ...