Advertisement
Slapoguzov

Untitled

Feb 21st, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <pthread.h>
  5.  
  6. #include "fcgi_config.h"
  7.  
  8. #include <stdlib.h>
  9.  
  10. #ifdef HAVE_UNISTD_H
  11. #include <unistd.h>
  12. #endif
  13.  
  14. #ifdef _WIN32
  15. #include <process.h>
  16. #else
  17. extern char **environ;
  18. #endif
  19.  
  20. #include "fcgi_stdio.h"
  21. #define THREAD_COUNT 8
  22. #define MAX_COM 10
  23.  
  24. int sock=-1;                        //дескриптор tcp
  25. char** coms;
  26. int countCom=MAX_COM;
  27.  
  28. static int sendall(int s, char *buf, int len, int flags)
  29. {
  30.     int total = 0;
  31.     int n;
  32.  
  33.     while(total < len)
  34.     {
  35.         n = send(s, buf+total, len-total, flags);
  36.         if(n == -1) { break; }
  37.         total += n;
  38.     }
  39.  
  40.     return (n==-1 ? -1 : total);
  41. }
  42.  
  43. static int createSocket()
  44. {
  45.     int listener;
  46.     struct sockaddr_in addr;
  47.     char buf[10];
  48.     char message[] = "hl car\0";
  49.  
  50.     listener = socket(AF_INET, SOCK_STREAM, 0);
  51.     if(listener < 0)
  52.     {
  53.         perror("socket");
  54.         exit(1);
  55.     }
  56.    
  57.     addr.sin_family = AF_INET;
  58.     addr.sin_port = htons(5858);
  59.     addr.sin_addr.s_addr = htonl(INADDR_ANY);
  60.     if(bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  61.     {
  62.         perror("bind");
  63.         exit(2);
  64.     }
  65.  
  66.     listen(listener, 1);
  67.    
  68.     while(1)
  69.     {
  70.         sock = accept(listener, NULL, NULL);
  71.         if(sock < 0)
  72.         {
  73.             perror("accept");
  74.             exit(3);
  75.         }
  76.        
  77.         send(sock, message, sizeof(message), 0);
  78.         recv(sock, buf, 9, 0);
  79.        
  80.         if(strstr(buf, "hl sr") != 0) return sock;
  81.        
  82.         return -1;
  83.     }
  84.    
  85.     return -1;
  86. }
  87. static void back_long()
  88. {
  89.    int i =0;
  90.    int count=0;
  91.    while(i < 100)
  92.    {
  93.    
  94.    if ( send(sock,"b0;", sizeof("b0;"), 0) == sizeof("b0;")) count++;
  95.    i++;
  96.    }
  97.    
  98.    printf("Content-type: text/html\r\n\r\n");
  99.    printf("<b>Отправлено: %d</b></br>", count);
  100. }
  101. static interCom(char* com)
  102. {
  103.    int i;
  104.    
  105.    if(countCom < 1) countCom=MAX_COM;
  106.    if(strstr(com, "cs") != 0)
  107.    {
  108.       if(sock == -1)
  109.       {
  110.       printf("Content-type: text/html\r\n");
  111.       printf("Create Connect, sock: %d\n", createSocket());
  112.       }
  113.    }
  114.    else if(strstr(com, "cls") != 0)
  115.    {
  116.       if(sock > 0)
  117.       {
  118.       printf("Content-type: text/html\r\n");
  119.       printf("Close sock: %d\n", sock);
  120.       close(sock);
  121.       sock=-1;
  122.       }
  123.    }
  124.    else if(strstr(com, "cm") != 0)
  125.    {
  126.          printf("Content-type: text/html\r\n\r\n");
  127.          printf("<b>Список команд</b></br><pre>");
  128.          for(i=MAX_COM; i > 0; i--)
  129.          {
  130.             printf("%s<br>", coms[i]);
  131.          }
  132.          printf("</pre>");
  133.    }
  134.    else if(strstr(com, "bb") != 0)
  135.    {
  136.       back_long();
  137.    }
  138.    else
  139.    {
  140.       coms[countCom] = malloc(sizeof(char)* 10);
  141.       sprintf(coms[countCom], "%s", com);
  142.       //send(sock,coms[countCom],sizeof(coms[countCom]),0);                
  143.       countCom--;
  144.    }
  145. }
  146.  
  147. static void *doit(void *a)
  148. //static void doit()
  149. {
  150.    
  151.    while (FCGI_Accept() >= 0) {
  152.        
  153.         char query[4];
  154.         sprintf(query,"%s", getenv("QUERY_STRING"));
  155.         interCom(query);
  156.    }
  157.    
  158. }
  159.  
  160. static void *sendCom(void *a)
  161. {
  162.    int i=MAX_COM;
  163.    while(1)
  164.    {
  165.       if(strstr(coms[i], ";") !=0)
  166.       {
  167.       send(sock,coms[i], sizeof(coms[i]), 0);
  168.       coms[i] = malloc(sizeof(char)* 10);
  169.       i--;
  170.       }
  171.       else
  172.       {
  173.          i=MAX_COM;
  174.       }
  175.    }
  176. }
  177.  
  178. int main ()
  179. {
  180.  
  181.     int i;
  182.     pthread_t id[2];
  183.     coms = (char**)malloc(MAX_COM * sizeof(char*));
  184.     for(i=0; i < MAX_COM; i++)
  185.     {
  186.     coms[i] = malloc(sizeof(char)* 10);
  187.     }
  188.    
  189.     pthread_create(&id[0], NULL, doit, NULL);
  190.     pthread_create(&id[1], NULL, sendCom, NULL);
  191.     pthread_join(id[0], NULL);
  192.     pthread_join(id[1], NULL);
  193.  
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement