Advertisement
zaxisss

Untitled

Jun 29th, 2022
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <fcntl.h>
  10. #include <ncurses.h>
  11.  
  12. #define SERV_PORT 45000
  13. #define BUFOR 1000
  14.  
  15. int id = -1;
  16. int linia = 0;
  17.  
  18. struct klient_inf{
  19.     struct sockaddr_in klient_address;
  20.     int klient_address_len;
  21.     int pipeIn[2];
  22.     int pipeOut[2];
  23. };
  24.  
  25. struct klient_inf klienci[256];
  26.  
  27. int main(){
  28.     initscr();
  29.     start_color();
  30.     wbkgd(stdscr, COLOR_PAIR(4));
  31.     init_pair(1, COLOR_WHITE, COLOR_BLACK);
  32.     init_pair(4, COLOR_BLACK, COLOR_GREEN);
  33.     attron(COLOR_PAIR(1));
  34.     mvprintw(linia, 2, "SERWER");
  35.     refresh();
  36.     int listenfd, connfd;
  37.     socklen_t klient_len;
  38.     struct sockaddr_in klient_addres, serwer_addres;
  39.     char Addr[100];
  40.     listenfd = socket(AF_INET, SOCK_DGRAM, 0);
  41.     bzero(&serwer_addres, sizeof(serwer_addres));
  42.     serwer_addres.sin_family = AF_INET;
  43.     serwer_addres.sin_addr.s_addr = htonl(INADDR_ANY);
  44.     serwer_addres.sin_port = htons(SERV_PORT);
  45.     struct timeval read_timeout;
  46.     read_timeout.tv_sec = 0;
  47.     read_timeout.tv_usec = 10;
  48.     setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof read_timeout);
  49.     bind(listenfd, &serwer_addres, sizeof(serwer_addres));
  50.     inet_ntop(AF_INET, &serwer_addres.sin_addr, Addr, 100);
  51.     for(;;) Echo(listenfd, klient_addres);
  52. }
  53.  
  54. void Echo(int socketfd, struct sockaddr_in klient_addres){
  55.     pid_t pid;
  56.     int n, i;
  57.     const int BUFSIZE = 4096;
  58.     char bufor[100];
  59.     char line[10000];
  60.     char Addr[100];
  61.     int len_klient_addres;
  62.     initscr();
  63.     start_color();
  64.     wbkgd(stdscr, COLOR_PAIR(4));
  65.     init_pair(1, COLOR_WHITE, COLOR_BLACK);
  66.     init_pair(4, COLOR_BLACK, COLOR_GREEN);
  67.     attron(COLOR_PAIR(1));
  68.     mvprintw(linia, 2, "SERWER");
  69.     //linia++;
  70.     for(;;)
  71.     {
  72.         n = recvfrom(socketfd, line, 100, 0, &klient_addres, &len_klient_addres);
  73.         if(n <= 0){
  74.             for(i=0; i < 255; i++){
  75.                 if(klienci[i].klient_address_len == 0)
  76.                     continue;
  77.                 while((n = read(klienci[i].pipeOut[0], bufor, 100)) > 0){
  78.                     sendto(socketfd, bufor, n, 0, &klienci[i].klient_address, klienci[i].klient_address_len);
  79.                
  80.                 }
  81.             }
  82.         }else{
  83.             line[n] = '\0';
  84.             inet_ntop(AF_INET, &klient_addres.sin_addr, Addr, 100);
  85.             if(n <= 1){
  86.                 mvprintw(linia, 1,"\nSERWER: POLACZENIE ZAKONCZONO");
  87.                 return;
  88.             }
  89.             if(line[0] == 0x00 && line[1] == 120){
  90.                 id++;
  91.         refresh();
  92.                 mvprintw(linia, 1,"\nPolaczono Klienta o adresie ID=[%s] Klient ID = [%d] \n",Addr,id);
  93.                 line[0] = id;
  94.                 sendto(socketfd, line, 1, 0, &klient_addres, sizeof(klient_addres));
  95.                 //Tworzenie uchwytów
  96.                 klienci[id].klient_address = klient_addres;
  97.                 klienci[id].klient_address_len = sizeof(klient_addres);
  98.                
  99.                 pipe(klienci[id].pipeOut);
  100.                 pipe(klienci[id].pipeIn);
  101.                 fcntl(klienci[id].pipeOut[0], F_SETFL, fcntl(klienci[i].pipeOut[0], F_GETFL) | O_NONBLOCK);
  102.                 pid = fork();
  103.                 if (pid < 0)
  104.                 {
  105.                     return 1;
  106.                 }else if(pid == 0){
  107.                     char innerbuf[100];
  108.                     int x = 0;
  109.                     while((x = read(klienci[id].pipeIn[0], innerbuf, 100)) > 0){
  110.                         innerbuf[x] = '\0';
  111.                         FILE *hook = popen(innerbuf, "r");
  112.                         char buffer[BUFSIZE];
  113.                         buffer[0] = 0x01;
  114.                         while(fgets(&buffer[1], BUFSIZE-1,hook)){
  115.                             printf(buffer);
  116.                             write(klienci[id].pipeOut[1], buffer, strlen(buffer));
  117.                            
  118.                         }
  119.                         buffer[0] = 0x02;
  120.                         write(klienci[id].pipeOut[1], buffer, 1);
  121.                         pclose(hook);
  122.                     }
  123.                     exit(0);
  124.                 }
  125.             }else{
  126.                 mvprintw(linia, 1, "\nSERWER: Wyslany z komputera o adresie IP=%s KlientID = [%d], zapytanie: %s\n", Addr,line[0],&line[1]);
  127.                 write(klienci[line[0]].pipeIn[1], &line[1], strlen(&line[1]));
  128.             }
  129.         }
  130.     }
  131.      mvprintw(linia, 1, "\nSERWER: Obsluga Klienta o adresie IP=%s zostala zakonczona", Addr);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement