5 |
5 |
6 #include <stdio.h> |
6 #include <stdio.h> |
7 #include <stdlib.h> |
7 #include <stdlib.h> |
8 #include <string.h> |
8 #include <string.h> |
9 #include <unistd.h> |
9 #include <unistd.h> |
|
10 #include <sys/types.h> |
10 #include <sys/socket.h> |
11 #include <sys/socket.h> |
11 #include <netinet/in.h> |
12 #include <netinet/in.h> |
|
13 #include <netdb.h> |
12 #include <arpa/inet.h> |
14 #include <arpa/inet.h> |
13 #include <sys/types.h> |
15 #include <sys/types.h> |
14 #include <signal.h> |
16 #include <signal.h> |
15 |
17 |
|
18 #define MAXLEN 80 |
|
19 |
16 int set_proc_limit (int); |
20 int set_proc_limit (int); |
|
21 |
|
22 int server_write (int client_socket, char *s) |
|
23 { |
|
24 return write (client_socket, s, strlen (s)); |
|
25 } |
|
26 |
|
27 int server_init (char *port) |
|
28 { |
|
29 int server_socket, status; |
|
30 struct addrinfo hints; |
|
31 struct addrinfo *result, *rp; |
|
32 |
|
33 int yes = 1; |
|
34 |
|
35 memset (&hints, 0, sizeof (hints)); |
|
36 hints.ai_family = AF_UNSPEC; |
|
37 hints.ai_socktype = SOCK_STREAM; |
|
38 hints.ai_flags = AI_PASSIVE; |
|
39 |
|
40 if (getaddrinfo (NULL, "4000", &hints, &result) < 0) { |
|
41 perror ("GETADDRINFO"); |
|
42 return -1; |
|
43 } |
|
44 |
|
45 server_socket = -1; |
|
46 for (rp = result; rp != NULL; rp = rp->ai_next) { |
|
47 if ((server_socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0) { |
|
48 perror ("SOCKET"); |
|
49 continue; |
|
50 } |
|
51 |
|
52 if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) { |
|
53 perror ("setsockopt"); |
|
54 return -1; |
|
55 } |
|
56 |
|
57 if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) { |
|
58 perror ("BIND"); |
|
59 close (server_socket); |
|
60 server_socket = -1; |
|
61 } else { |
|
62 printf ("bound to an %s socket\n", (rp->ai_family == AF_INET6) ? "IPv6" : "IPv4"); |
|
63 } |
|
64 } |
|
65 |
|
66 status = listen (server_socket, 10); |
|
67 |
|
68 if (status != 0) { |
|
69 perror ("LISTEN"); |
|
70 return -1; |
|
71 } |
|
72 |
|
73 if (result != NULL) |
|
74 freeaddrinfo (result); |
|
75 |
|
76 return server_socket; |
|
77 } |
17 |
78 |
18 int main (int argc, char **argv) |
79 int main (int argc, char **argv) |
19 { |
80 { |
20 char message[81]; |
|
21 int server_socket; |
81 int server_socket; |
22 int client_socket; |
82 int client_socket; |
23 struct sockaddr_in sa; |
83 |
24 struct sockaddr_in ca; |
84 char message[MAXLEN + 1]; |
25 socklen_t size; |
|
26 int len; |
85 int len; |
27 int status; |
86 |
28 pid_t pid; |
87 pid_t pid; |
29 pid_t parent_pid; |
88 pid_t parent_pid; |
30 |
89 |
31 server_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
90 if ((server_socket = server_init ("4000")) < 0) |
32 if (server_socket == -1) { |
|
33 perror ("socket"); |
|
34 return EXIT_FAILURE; |
91 return EXIT_FAILURE; |
35 } |
|
36 |
|
37 sa.sin_family = AF_INET; |
|
38 sa.sin_port = htons (4000); |
|
39 sa.sin_addr.s_addr = INADDR_ANY; |
|
40 |
|
41 size = sizeof (sa); |
|
42 |
|
43 status = bind (server_socket, (struct sockaddr *)&sa, size); |
|
44 if (status != 0) { |
|
45 perror ("BIND"); |
|
46 return EXIT_FAILURE; |
|
47 } |
|
48 |
|
49 status = listen (server_socket, 10); |
|
50 if (status != 0) { |
|
51 perror ("LISTEN"); |
|
52 return EXIT_FAILURE; |
|
53 } |
|
54 |
92 |
55 int links = 0; |
93 int links = 0; |
56 |
94 |
57 parent_pid = getpid (); |
95 parent_pid = getpid (); |
58 |
96 |
65 printf ("Cannot limit the process limit\n"); |
103 printf ("Cannot limit the process limit\n"); |
66 return EXIT_FAILURE; |
104 return EXIT_FAILURE; |
67 } |
105 } |
68 |
106 |
69 for (;;) { |
107 for (;;) { |
70 size = sizeof (ca); |
108 client_socket = accept (server_socket, NULL, NULL); |
71 client_socket = accept (server_socket, (struct sockaddr *)&sa, (socklen_t *) & size); |
|
72 |
109 |
73 printf ("PARENT PID = %d\n", parent_pid); |
110 printf ("PARENT PID = %d\n", parent_pid); |
74 |
111 |
75 pid = fork (); |
112 pid = fork (); |
76 switch (pid) { |
113 switch (pid) { |
77 case 0: /* Child */ |
114 case 0: /* Child */ |
78 close (server_socket); |
115 close (server_socket); |
79 if ((len = read (client_socket, message, 80)) == -1) |
116 if ((len = read (client_socket, message, MAXLEN)) == -1) |
80 break; |
117 break; |
81 message[len] = 0; |
118 message[len] = '\0'; |
82 |
119 |
83 len = write (client_socket, "Please wait...\r\n", 17); |
120 len = server_write (client_socket, "Please wait...\r\n"); |
84 sleep (10); |
121 sleep (10); |
85 |
122 |
86 printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
123 printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
87 shutdown (client_socket, SHUT_RDWR); |
124 shutdown (client_socket, SHUT_RDWR); |
88 close (client_socket); |
125 close (client_socket); |
89 return 1; |
126 return 1; |
90 case -1: |
127 case -1: |
91 perror ("Fork Error"); |
128 perror ("Fork Error"); |
92 close (client_socket); |
129 close (client_socket); |
93 /* |
130 /* |
94 * server is still alive and will continue |
131 * server is still alive and will continue when resources are available |
95 * * when resources are available |
|
96 */ |
132 */ |
97 break; |
133 break; |
98 default: /* PID > 0 */ |
134 default: /* PID > 0 */ |
99 close (client_socket); |
135 close (client_socket); |
100 printf ("Starting LINK [%04d] %04d\n", pid, links++); |
136 printf ("Starting LINK [%04d] %04d\n", pid, links++); |