6 #include <sys/socket.h>
7 #include <netinet/in.h>
14 read_from_client (int filedes
)
19 nbytes
= read (filedes
, buffer
, MAXMSG
);
32 fprintf (stderr
, "Server: got message: `%s'\n", buffer
);
40 extern int make_socket (uint16_t port
);
42 fd_set active_fd_set
, read_fd_set
;
44 struct sockaddr_in clientname
;
47 /* Create the socket and set it up to accept connections. */
48 sock
= make_socket (PORT
);
49 if (listen (sock
, 1) < 0)
55 /* Initialize the set of active sockets. */
56 FD_ZERO (&active_fd_set
);
57 FD_SET (sock
, &active_fd_set
);
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)
69 /* Service all the sockets with input pending. */
70 for (i
= 0; i
< FD_SETSIZE
; ++i
)
71 if (FD_ISSET (i
, &read_fd_set
))
75 /* Connection request on original socket. */
77 size
= sizeof (clientname
);
79 (struct sockaddr
*) &clientname
,
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
);
94 /* Data arriving on an already-connected socket. */
95 if (read_from_client (i
) < 0)
98 FD_CLR (i
, &active_fd_set
);