Guest User

Server_program_without_SSL

a guest
Mar 12th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. /*  For our final example, server5.c,
  2.     we include the sys/time.h and sys/ioctl.h headers in place of signal.h
  3.     in our last program and declare some extra variables to deal with select.  */
  4.  
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <stdio.h>
  8. #include <netinet/in.h>
  9. #include <sys/time.h>
  10. #include <sys/ioctl.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13.  
  14. int main()
  15. {
  16.     int server_sockfd, client_sockfd;
  17.     int server_len, client_len;
  18.     struct sockaddr_in server_address;
  19.     struct sockaddr_in client_address;
  20.     int result;
  21.     fd_set readfds, testfds;
  22.  
  23. /*  Create and name a socket for the server.  */
  24.  
  25.     server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  26.     printf("server_sockfd = %d\n", server_sockfd);
  27.  
  28.     server_address.sin_family = AF_INET;
  29.     server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  30.     server_address.sin_port = htons(9734);
  31.     server_len = sizeof(server_address);
  32.  
  33.     bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
  34.  
  35. /*  Create a connection queue and initialize readfds to handle input from server_sockfd.  */
  36.  
  37.     listen(server_sockfd, 5);
  38.  
  39.     FD_ZERO(&readfds);
  40.     FD_SET(server_sockfd, &readfds);
  41.  
  42. /*  Now wait for clients and requests.
  43.     Since we have passed a null pointer as the timeout parameter, no timeout will occur.
  44.     The program will exit and report an error if select returns a value of less than 1.  */
  45.  
  46.     int counter;
  47.     counter = 0;
  48.     while(1) {
  49.         char ch;
  50.         int fd;
  51.         int nread;
  52.  
  53.     counter++;
  54.     printf("counter = %d\n", counter);
  55.         testfds = readfds;
  56.  
  57.     printf("testfds = %d\n", testfds);
  58.     printf("readfds = %d\n", readfds);
  59.  
  60.         printf("server waiting\n");
  61.         result = select(FD_SETSIZE, &testfds, (fd_set *)0,
  62.             (fd_set *)0, (struct timeval *) 0);
  63.  
  64.         if(result < 1) {
  65.             perror("server5");
  66.             exit(1);
  67.         }
  68.  
  69. /*  Once we know we've got activity,
  70.     we find which descriptor it's on by checking each in turn using FD_ISSET.  */
  71.  
  72.         for(fd = 0; fd < FD_SETSIZE; fd++) {
  73.             if(FD_ISSET(fd,&testfds)) {
  74.  
  75. /*  If the activity is on server_sockfd, it must be a request for a new connection
  76.     and we add the associated client_sockfd to the descriptor set.  */
  77.  
  78.                 if(fd == server_sockfd) {
  79.                     client_len = sizeof(client_address);
  80.                     client_sockfd = accept(server_sockfd,
  81.                         (struct sockaddr *)&client_address, &client_len);
  82.                     FD_SET(client_sockfd, &readfds);
  83.                     printf("adding client on fd %d\n", client_sockfd);
  84.                 }
  85.  
  86. /*  If it isn't the server, it must be client activity.
  87.     If close is received, the client has gone away and we remove it from the descriptor set.
  88.     Otherwise, we 'serve' the client as in the previous examples.  */
  89.  
  90.                 else {
  91.                     ioctl(fd, FIONREAD, &nread);
  92.             printf("nread = %d\n", nread);
  93.  
  94.  
  95.                     if(nread == 0) {
  96.                         close(fd);
  97.                         FD_CLR(fd, &readfds);
  98.                         printf("removing client on fd %d\n", fd);
  99.                     }
  100.  
  101.                     else {
  102.                         read(fd, &ch, 1);
  103.                         sleep(5);
  104.                         printf("serving client on fd %d\n", fd);
  105.                         ch++;
  106.                         write(fd, &ch, 1);
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
Add Comment
Please, Sign In to add comment