Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  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>
  9.  
  10. #define PORT    5000
  11. #define MAXMSG  512
  12.  
  13. int
  14.     read_from_client (int filedes)
  15. {
  16.     char buffer[MAXMSG];
  17.     int nbytes;
  18.     nbytes = read (filedes, buffer, MAXMSG);
  19.     if (nbytes < 0)
  20.     {
  21.         /* Read error. */
  22.         perror ("read");
  23.         exit (EXIT_FAILURE);
  24.     }
  25.     else if (nbytes == 0)
  26.         /* End-of-file. */
  27.         return -1;
  28.     else
  29.     {
  30.         /* Data read. */
  31.         printf ("Server: got message: `%s'\n", buffer);
  32.         //fprintf (stderr, "Server: got message: `%s'\n", buffer);
  33.         return 0;
  34.     }
  35. }
  36.  
  37.  
  38. int main (void)
  39. {
  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. ;
  46.     //size_t size;
  47.     int size;
  48.  
  49.     /* Create the socket and set it up to accept connections. */
  50.     //sock = make_socket (PORT);
  51.     if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  52.     {
  53.         perror("Server-socket() error lol!");
  54.         /*just exit lol!*/
  55.         exit(1);
  56.     }
  57.     printf("Server-socket() is OK...\n");
  58.  
  59.  
  60.  
  61.     /*"address already in use" error message */
  62.     int yes=1;
  63.     if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  64.     {
  65.     perror("Server-setsockopt() error lol!");
  66.     exit(1);
  67.     }
  68.     printf("Server-setsockopt() is OK...\n");
  69.    
  70.  
  71.     /* bind */
  72.     clientname.sin_family = AF_INET;
  73.     clientname.sin_addr.s_addr = INADDR_ANY;
  74.     clientname.sin_port = htons(PORT);
  75.     //memset(clientname.sin_zero, '\0', 8);
  76.      
  77.     if(bind(sock, (struct sockaddr *)&clientname, sizeof(clientname)) == -1)
  78.     {
  79.         perror("Server-bind() error lol!");
  80.         exit(1);
  81.     }
  82.     printf("Server-bind() is OK...\n");
  83.  
  84.  
  85.     if (listen (sock, 1) < 0)
  86.     {
  87.         perror ("listen");
  88.         exit (EXIT_FAILURE);
  89.     }
  90.  
  91.     /* Initialize the set of active sockets. */
  92.     FD_ZERO (&active_fd_set);
  93.     FD_SET (sock, &active_fd_set);
  94.  
  95.     while (1)
  96.     {
  97.         /* Block until input arrives on one or more active sockets. */
  98.         read_fd_set = active_fd_set;
  99.         if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
  100.         {
  101.             perror ("select");
  102.             exit (EXIT_FAILURE);
  103.         }
  104.  
  105.         /* Service all the sockets with input pending. */
  106.         for (i = 0; i < FD_SETSIZE; ++i)
  107.             if (FD_ISSET (i, &read_fd_set))
  108.         {
  109.             if (i == sock)
  110.             {
  111.                 /* Connection request on original socket. */
  112.                 int new;
  113.                 size = sizeof (clientname);
  114.                 new = accept (sock,  (struct sockaddr *) &clientname, & size);
  115.                 if (new < 0)
  116.                        
  117.                 {
  118.                     perror ("accept");
  119.                     exit (EXIT_FAILURE);
  120.                 }
  121.                
  122.                
  123.  
  124.  
  125.                 FD_SET (new, &active_fd_set);
  126.             }
  127.             else
  128.             {
  129.                 /* Data arriving on an already-connected socket. */
  130.                 if (read_from_client (i) < 0)
  131.                 {
  132.                     //close (i);
  133.                     //FD_CLR (i, &active_fd_set);
  134.                 }
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement