Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <netinet/in.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <sys/time.h>
  10. #include <sys/socket.h>
  11. #include <sys/uio.h>
  12. #include <arpa/inet.h>
  13.  
  14. struct clientinfo {
  15.    int fd;
  16.    struct sockaddr_in info;
  17. };
  18.  
  19. int rmfd(struct clientinfo *connects, int location, int numcon) {
  20.    int count;
  21.    struct clientinfo *temp;
  22.  
  23.    for(count=location;count<=numcon;count++)
  24.       connects[count] = connects[count+1];
  25.  
  26.    temp = malloc(sizeof(struct clientinfo)*(numcon-1+(numcon-1==0)));
  27.  
  28.    for(count=0;count<numcon-1;count++) {
  29.       temp[count] = connects[count];
  30.    }
  31.  
  32.    connects = malloc(sizeof(struct clientinfo)*(numcon-1+(numcon-1==0)));
  33.    for(count=0;count<numcon-1;count++) {
  34.       connects[count] = temp[count];
  35.    }
  36.  
  37.    return numcon-1;
  38. }
  39.  
  40. int addfd(struct clientinfo *connects, int numcon) {
  41.    struct clientinfo *temp;
  42.    int count;
  43.    temp = malloc(sizeof(struct clientinfo) * (numcon+(numcon==0)));
  44.    for(count=0;count<numcon;count++) {
  45.       temp[count].fd = connects[count].fd;
  46.       temp[count].info = connects[count].info;
  47.    }
  48.    connects = malloc(sizeof(struct clientinfo)*(numcon+1));
  49.    for(count=0;count<numcon;count++) {
  50.       connects[count].fd = temp[count].fd;
  51.  
  52.       connects[count].info = temp[count].info;
  53.    }
  54.    return numcon+1;
  55. }
  56.  
  57. int main(int argc, char **argv) {
  58.    int check;
  59.    int sin_size;
  60.    int numcon;
  61.    int maxfd;
  62.    int readtest;
  63.    int writecount;
  64.    char rwbuf[1024];
  65.    struct timeval timer;
  66.    struct clientinfo *connects;
  67.    fd_set sockrd;
  68.  
  69.    if (argc != 2) {
  70.       fprintf(stderr, "usage: %s port\n", argv[0]);
  71.       exit(-1);
  72.    }
  73.  
  74.    sin_size = sizeof(struct sockaddr);
  75.    numcon = 0;
  76.    connects = malloc(sizeof(struct clientinfo));
  77.    FD_ZERO(&sockrd);
  78.    connects[0].info.sin_family = AF_INET;
  79.    connects[0].info.sin_port = htons(atoi(argv[1]));
  80.    connects[0].info.sin_addr.s_addr = INADDR_ANY;
  81.  
  82.    connects[0].fd = socket(AF_INET, SOCK_STREAM, 0);
  83.    check = bind(connects[0].fd, (struct sockaddr *)&connects[0].info, sizeof(struct sockaddr));
  84.    if(check == -1) {
  85.       perror("bind");
  86.       exit(check);
  87.    }
  88.    check = listen(connects[0].fd, 1024);
  89.    if (check == -1) {
  90.       perror("listen");
  91.       exit(check);
  92.    }
  93.    maxfd = connects[0].fd;
  94.    for(;;) {
  95.       FD_ZERO(&sockrd);
  96.       FD_SET(connects[0].fd, &sockrd);
  97.       for(check=1;check<=numcon;check++)
  98.          FD_SET(connects[check].fd, &sockrd);
  99.       timer.tv_sec = 60;
  100.       timer.tv_usec = 0;
  101.  
  102.       select(maxfd+1, &sockrd, NULL, NULL, &timer);
  103.       if (FD_ISSET(connects[0].fd, &sockrd)) {
  104.          sin_size = sizeof(struct sockaddr_in);
  105.          numcon = addfd(connects, numcon);
  106.          connects[numcon].fd = accept(connects[0].fd, (struct sockaddr *)&connects[numcon].info, &sin_size);
  107.          if (connects[numcon].fd > maxfd)
  108.             maxfd = connects[numcon].fd;
  109.       }
  110.       else {
  111.          for(readtest=1;readtest<=numcon;readtest++) {
  112.             if(FD_ISSET(connects[readtest].fd, &sockrd)) {
  113.                memset(rwbuf, 0, 1024);
  114.                check = read(connects[readtest].fd, rwbuf, 1024);
  115.                if (check == 0) {
  116.                   close(connects[readtest].fd);
  117.                   numcon = rmfd(connects, readtest, numcon);
  118.                }
  119.                else {
  120.                   for(writecount=1;writecount<=numcon;writecount++) {
  121.                      if (writecount != readtest)
  122.                         write(connects[writecount].fd, rwbuf, 1024);
  123.                   }
  124.                }
  125.             }
  126.          }
  127.       }
  128.    }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement