Advertisement
Guest User

server.c

a guest
Mar 29th, 2010
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7. #include <unistd.h>
  8.  
  9. #define MSG_SIZE 1024
  10. #define LISTEN_Q 32
  11.  
  12. struct sockFDList   {
  13.     int sockFD;
  14.    
  15.     struct sockFDList *next;
  16. };
  17.  
  18. int main(int argc, char *argv[])    {
  19.     int listenFD, newFD, fdmax, i;      // listenFD for listening, newFD for new connection
  20.     char msg[MSG_SIZE];         // buffer for msg transmit
  21.     struct addrinfo hints, *myAI;       // address info
  22.     struct sockaddr_storage remoteAddr; // store client address
  23.     socklen_t sizeAddr;         // sizeof client address
  24.     fd_set masterSet, readSet;      // FD sets for list and read.
  25.    
  26.     // Clear Sets
  27.     FD_ZERO(&masterSet);
  28.     FD_ZERO(&readSet);
  29.    
  30.     // Clear hints
  31.     memset(&hints, 0, sizeof hints);
  32.  
  33.     // Prep hints
  34.     hints.ai_family = AF_INET;
  35.     hints.ai_socktype = SOCK_STREAM;
  36.     hints.ai_flags = AI_PASSIVE;
  37.  
  38.     // Get local addr info
  39.     getaddrinfo("localhost", argv[1], &hints, &myAI);
  40.  
  41.     // Get socket for listening
  42.     listenFD = socket(myAI->ai_family, myAI->ai_socktype, myAI->ai_protocol);
  43.  
  44.     // Bind socket sockFD
  45.     bind(listenFD, myAI->ai_addr, myAI->ai_addrlen);
  46.    
  47.     // Add sfd to master set
  48.     FD_SET(listenFD, &masterSet);
  49.    
  50.     // Initialize fdmax
  51.     fdmax = listenFD;
  52.    
  53.    
  54.     while(1)    {
  55.         // Copy master
  56.         readSet = masterSet;
  57.        
  58.         // call select
  59.         if(select(fdmax+1, &readSet, NULL, NULL, NULL) == -1)
  60.             continue;
  61.        
  62.         // loop through readSet
  63.         for(i=0; i<=fdmax; i++) {
  64.             if(FD_ISSET(i, &readSet))   {   // Active !
  65.                 if(i == listenFD)   {   // If listener socket
  66.                     // New Connection, accept
  67.                     sizeAddr = sizeof remoteAddr;
  68.                     newFD = accept(listenFD, (struct sockaddr *)&remoteAddr, &sizeAddr);
  69.                     printf("\n");                  
  70.                    
  71.                     // Add this connection to my master list
  72.                     FD_SET(newFD, &masterSet);
  73.  
  74.                     // update fdmax
  75.                     if(newFD > fdmax)
  76.                         fdmax = newFD;     
  77.                 }
  78.                 else    {
  79.                     // Something to read on a socket
  80.                     recv(i, msg, MSG_SIZE, 0);
  81.                     msg[MSG_SIZE] = '\0';      
  82.  
  83.                     // print it.
  84.                     printf("%s\n",msg);
  85.                 }
  86.             }
  87.         }  
  88.     }
  89.    
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement