Slicksilver555

Untitled

Oct 25th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <ctype.h>
  8. #include <sys/time.h>
  9. #include <fcntl.h>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <unistd.h>
  13.  
  14.  
  15. int sock;
  16.  
  17. int connectlist[5];
  18.  
  19. fd_set socks;
  20.  
  21. int highsock;
  22.  
  23. void setnonblocking(int sock)
  24. {
  25.     int opts;
  26.  
  27.     opts = fcntl(sock, F_GETFL);
  28.     if(opts < 0)
  29.     {
  30.         perror("fcntl(F_GETFL)");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     opts = (opts | O_NONBLOCK);
  34.     if(fcntl(sock, F_SETFL, opts) < 0)
  35.     {
  36.         perror("fcntl(F_SETFL)");
  37.         exit(EXIT_FAILURE);
  38.     }
  39.     return;
  40. }
  41.  
  42. void build_select_list()
  43. {
  44.     int listnum = 0;
  45.  
  46.     FD_ZERO(&socks);
  47.     FD_SET(sock, &socks);
  48.  
  49.     while(connectlist[listnum] != 0)
  50.     {
  51.         FD_SET(connectlist[listnum], &socks);
  52.         if(connectlist[listnum] > highsock)
  53.                 highsock = connectlist[listnum++];
  54.     }
  55. }
  56.  
  57. void handle_new_connection()
  58. {
  59.     int listnum;
  60.     int connection;
  61.  
  62.     connection = accept(sock, NULL, NULL);
  63.     if(connection < 0)
  64.     {
  65.         perror("accept");
  66.         exit(EXIT_FAILURE);
  67.     }
  68.     setnonblocking(connection);
  69.     while(connection != -1)
  70.         if(connectlist[listnum] == 0)
  71.         {
  72.             printf("Connection accepted:\nFD=%d;\nSlot=%d\n", connection, listnum);
  73.             connectlist[listnum++] = connection;
  74.             connection = -1;
  75.         }
  76. }
  77.  
  78. void handle_data(int listnum)
  79. {
  80.     char buffer[256];
  81.  
  82.     if(read(connectlist[listnum], buffer, 256) < 0)
  83.     {
  84.         printf("\nConnection lost:\nFD=%d;\nSlot=%d\n", connectlist[listnum], listnum);
  85.         close(connectlist[listnum]);
  86.         connectlist[listnum] = 0;
  87.     }
  88.     else
  89.     {
  90.         printf("\nRecieved: %s; ", buffer);
  91.         write(connectlist[listnum], buffer, sizeof(buffer));
  92.         write(connectlist[listnum], "\n", sizeof("\n"));
  93.         printf("Responded.\n");
  94.     }
  95. }
  96.  
  97. void read_socks()
  98. {
  99.     int listnum;
  100.  
  101.     if(FD_ISSET(sock, &socks))
  102.         handle_new_connection();
  103.     while(connectlist[listnum] != 0)
  104.         if(FD_ISSET(connectlist[listnum++], &socks))
  105.             handle_data(listnum-1);
  106. }
  107.  
  108. int main(int argc, char* argv[])
  109. {
  110.     printf("maybs\n");
  111.     char *ascport;
  112.     int port;
  113.     struct sockaddr_in server_address;
  114.     int reuse_addr = 1;
  115.     struct timeval timeout;
  116.     int readsocks;
  117.     if(argc < 2)
  118.     {
  119.         printf("Usage: %s PORT\r\n", argv[0]);
  120.         exit(EXIT_FAILURE);
  121.     }
  122.  
  123.     sock = socket(AF_INET, SOCK_STREAM, 0);
  124.     if(sock < 0)
  125.     {
  126.         perror("SOCKET");
  127.         exit(EXIT_FAILURE);
  128.     }
  129.     printf("com on yo\n");
  130.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
  131.  
  132.     setnonblocking(sock);
  133.  
  134.     ascport = argv[1];
  135.     port = atoi(ascport);
  136.  
  137.     memset((char *) &server_address, 0, sizeof(server_address));
  138.     server_address.sin_family = AF_INET;
  139.     server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  140.     server_address.sin_port = port;
  141.  
  142.     if(bind(sock, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
  143.     {
  144.         perror("BIND");
  145.         close(sock);
  146.         exit(EXIT_FAILURE);
  147.     }
  148.     printf("past bind?\n");
  149.     listen(sock, 1024);
  150.  
  151.     highsock = sock;
  152.     memset((char *) &connectlist, 0, sizeof(connectlist));
  153.  
  154.     while(1) {
  155.  
  156.         build_select_list();
  157.  
  158.         timeout.tv_sec = 1;
  159.         timeout.tv_usec = 0;
  160.  
  161.         readsocks = select(highsock+1, &socks, (fd_set *) 0, (fd_set *) 0, &timeout);
  162.  
  163.         if(readsocks < 0)
  164.         {
  165.             perror("select");
  166.             exit(EXIT_FAILURE);
  167.         }
  168.         if(readsocks != 0)
  169.             read_socks();
  170.         printf("nothin 2 do\n");
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment