Allow variable shift values in mmap2.
[glibc.git] / manual / examples / inetsrv.c
blob3d544c005cbe27898e436da499e45ce9e82d4e0c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
10 #define PORT 5555
11 #define MAXMSG 512
13 int
14 read_from_client (int filedes)
16 char buffer[MAXMSG];
17 int nbytes;
19 nbytes = read (filedes, buffer, MAXMSG);
20 if (nbytes < 0)
22 /* Read error. */
23 perror ("read");
24 exit (EXIT_FAILURE);
26 else if (nbytes == 0)
27 /* End-of-file. */
28 return -1;
29 else
31 /* Data read. */
32 fprintf (stderr, "Server: got message: `%s'\n", buffer);
33 return 0;
37 int
38 main (void)
40 extern int make_socket (uint16_t port);
41 int sock;
42 fd_set active_fd_set, read_fd_set;
43 int i;
44 struct sockaddr_in clientname;
45 size_t size;
47 /* Create the socket and set it up to accept connections. */
48 sock = make_socket (PORT);
49 if (listen (sock, 1) < 0)
51 perror ("listen");
52 exit (EXIT_FAILURE);
55 /* Initialize the set of active sockets. */
56 FD_ZERO (&active_fd_set);
57 FD_SET (sock, &active_fd_set);
59 while (1)
61 /* Block until input arrives on one or more active sockets. */
62 read_fd_set = active_fd_set;
63 if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
65 perror ("select");
66 exit (EXIT_FAILURE);
69 /* Service all the sockets with input pending. */
70 for (i = 0; i < FD_SETSIZE; ++i)
71 if (FD_ISSET (i, &read_fd_set))
73 if (i == sock)
75 /* Connection request on original socket. */
76 int new;
77 size = sizeof (clientname);
78 new = accept (sock,
79 (struct sockaddr *) &clientname,
80 &size);
81 if (new < 0)
83 perror ("accept");
84 exit (EXIT_FAILURE);
86 fprintf (stderr,
87 "Server: connect from host %s, port %hd.\n",
88 inet_ntoa (clientname.sin_addr),
89 ntohs (clientname.sin_port));
90 FD_SET (new, &active_fd_set);
92 else
94 /* Data arriving on an already-connected socket. */
95 if (read_from_client (i) < 0)
97 close (i);
98 FD_CLR (i, &active_fd_set);