Back to index

glibc  2.9
inetsrv.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <errno.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <sys/types.h>
00006 #include <sys/socket.h>
00007 #include <netinet/in.h>
00008 #include <netdb.h>
00009 
00010 #define PORT  5555
00011 #define MAXMSG       512
00012 
00013 int
00014 read_from_client (int filedes)
00015 {
00016   char buffer[MAXMSG];
00017   int nbytes;
00018 
00019   nbytes = read (filedes, buffer, MAXMSG);
00020   if (nbytes < 0)
00021     {
00022       /* Read error. */
00023       perror ("read");
00024       exit (EXIT_FAILURE);
00025     }
00026   else if (nbytes == 0)
00027     /* End-of-file. */
00028     return -1;
00029   else
00030     {
00031       /* Data read. */
00032       fprintf (stderr, "Server: got message: `%s'\n", buffer);
00033       return 0;
00034     }
00035 }
00036 
00037 int
00038 main (void)
00039 {
00040   extern int make_socket (uint16_t port);
00041   int sock;
00042   fd_set active_fd_set, read_fd_set;
00043   int i;
00044   struct sockaddr_in clientname;
00045   size_t size;
00046 
00047   /* Create the socket and set it up to accept connections. */
00048   sock = make_socket (PORT);
00049   if (listen (sock, 1) < 0)
00050     {
00051       perror ("listen");
00052       exit (EXIT_FAILURE);
00053     }
00054 
00055   /* Initialize the set of active sockets. */
00056   FD_ZERO (&active_fd_set);
00057   FD_SET (sock, &active_fd_set);
00058 
00059   while (1)
00060     {
00061       /* Block until input arrives on one or more active sockets. */
00062       read_fd_set = active_fd_set;
00063       if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
00064        {
00065          perror ("select");
00066          exit (EXIT_FAILURE);
00067        }
00068 
00069       /* Service all the sockets with input pending. */
00070       for (i = 0; i < FD_SETSIZE; ++i)
00071        if (FD_ISSET (i, &read_fd_set))
00072          {
00073            if (i == sock)
00074              {
00075               /* Connection request on original socket. */
00076               int new;
00077               size = sizeof (clientname);
00078               new = accept (sock,
00079                            (struct sockaddr *) &clientname,
00080                            &size);
00081               if (new < 0)
00082                 {
00083                   perror ("accept");
00084                   exit (EXIT_FAILURE);
00085                 }
00086               fprintf (stderr,
00087                       "Server: connect from host %s, port %hd.\n",
00088                       inet_ntoa (clientname.sin_addr),
00089                       ntohs (clientname.sin_port));
00090               FD_SET (new, &active_fd_set);
00091              }
00092            else
00093              {
00094               /* Data arriving on an already-connected socket. */
00095               if (read_from_client (i) < 0)
00096                 {
00097                   close (i);
00098                   FD_CLR (i, &active_fd_set);
00099                 }
00100              }
00101          }
00102     }
00103 }