Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <pthread.h>
- #include "fcgi_config.h"
- #include <stdlib.h>
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef _WIN32
- #include <process.h>
- #else
- extern char **environ;
- #endif
- #include "fcgi_stdio.h"
- #define THREAD_COUNT 8
- #define MAX_COM 10
- int sock=-1; //дескриптор tcp
- char** coms;
- int countCom=MAX_COM;
- static int sendall(int s, char *buf, int len, int flags)
- {
- int total = 0;
- int n;
- while(total < len)
- {
- n = send(s, buf+total, len-total, flags);
- if(n == -1) { break; }
- total += n;
- }
- return (n==-1 ? -1 : total);
- }
- static int createSocket()
- {
- int listener;
- struct sockaddr_in addr;
- char buf[10];
- char message[] = "hl car\0";
- listener = socket(AF_INET, SOCK_STREAM, 0);
- if(listener < 0)
- {
- perror("socket");
- exit(1);
- }
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5858);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- if(bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0)
- {
- perror("bind");
- exit(2);
- }
- listen(listener, 1);
- while(1)
- {
- sock = accept(listener, NULL, NULL);
- if(sock < 0)
- {
- perror("accept");
- exit(3);
- }
- send(sock, message, sizeof(message), 0);
- recv(sock, buf, 9, 0);
- if(strstr(buf, "hl sr") != 0) return sock;
- return -1;
- }
- return -1;
- }
- static void back_long()
- {
- int i =0;
- int count=0;
- while(i < 100)
- {
- if ( send(sock,"b0;", sizeof("b0;"), 0) == sizeof("b0;")) count++;
- i++;
- }
- printf("Content-type: text/html\r\n\r\n");
- printf("<b>Отправлено: %d</b></br>", count);
- }
- static interCom(char* com)
- {
- int i;
- if(countCom < 1) countCom=MAX_COM;
- if(strstr(com, "cs") != 0)
- {
- if(sock == -1)
- {
- printf("Content-type: text/html\r\n");
- printf("Create Connect, sock: %d\n", createSocket());
- }
- }
- else if(strstr(com, "cls") != 0)
- {
- if(sock > 0)
- {
- printf("Content-type: text/html\r\n");
- printf("Close sock: %d\n", sock);
- close(sock);
- sock=-1;
- }
- }
- else if(strstr(com, "cm") != 0)
- {
- printf("Content-type: text/html\r\n\r\n");
- printf("<b>Список команд</b></br><pre>");
- for(i=MAX_COM; i > 0; i--)
- {
- printf("%s<br>", coms[i]);
- }
- printf("</pre>");
- }
- else if(strstr(com, "bb") != 0)
- {
- back_long();
- }
- else
- {
- coms[countCom] = malloc(sizeof(char)* 10);
- sprintf(coms[countCom], "%s", com);
- //send(sock,coms[countCom],sizeof(coms[countCom]),0);
- countCom--;
- }
- }
- static void *doit(void *a)
- //static void doit()
- {
- while (FCGI_Accept() >= 0) {
- char query[4];
- sprintf(query,"%s", getenv("QUERY_STRING"));
- interCom(query);
- }
- }
- static void *sendCom(void *a)
- {
- int i=MAX_COM;
- while(1)
- {
- if(strstr(coms[i], ";") !=0)
- {
- send(sock,coms[i], sizeof(coms[i]), 0);
- coms[i] = malloc(sizeof(char)* 10);
- i--;
- }
- else
- {
- i=MAX_COM;
- }
- }
- }
- int main ()
- {
- int i;
- pthread_t id[2];
- coms = (char**)malloc(MAX_COM * sizeof(char*));
- for(i=0; i < MAX_COM; i++)
- {
- coms[i] = malloc(sizeof(char)* 10);
- }
- pthread_create(&id[0], NULL, doit, NULL);
- pthread_create(&id[1], NULL, sendCom, NULL);
- pthread_join(id[0], NULL);
- pthread_join(id[1], NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement