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 <netdb.h>
- #include <memory.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- #include <signal.h>
- #include <pthread.h>
- #define SRV_PORT 13002 //порт сервера
- #define BUF_SIZE 128
- pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
- struct thread_arg
- {
- int s_client;
- char buf[128];
- };
- void* calculation(void *arg)
- {
- int from_len;
- char buf[128];
- pthread_mutex_lock(&lock);
- struct thread_arg parg = *(struct thread_arg*)(arg);
- pthread_mutex_unlock(&lock);
- static char* output;
- int new_socket = parg.s_client;
- //output = parg.buf;
- from_len = recv(new_socket, buf, BUF_SIZE, 0);
- sleep(10);
- pthread_mutex_lock(&lock);
- FILE *result = popen (buf,"r");
- fread(buf,sizeof(char), 256, result);
- printf("%s\n",buf);
- pthread_mutex_unlock(&lock);
- send(new_socket, buf, 256, 0);
- //buf[0]='\0';
- //shutdown(new_socket, 0);
- //close(new_socket);
- }
- int main()
- {
- int s, s_client;
- int from_len;
- char buf[BUF_SIZE];
- int len_buf;
- char output[128];
- pthread_t thread;
- pthread_t tid[50];
- struct sockaddr_in sin, from_sin;
- s = socket(AF_INET, SOCK_STREAM, 0);
- memset((char*)&sin, '\0', sizeof(sin));
- sin.sin_family = AF_INET; // так как используем TCP/IP
- sin.sin_addr.s_addr = INADDR_ANY; //привязываем сокет к номеру локального узла сети
- sin.sin_port = SRV_PORT; //номер порта сервера
- bind (s, (struct sockaddr *)&sin, sizeof(sin)); //связываем сокет
- listen(s, 50); // ждем 1 запроса
- while(1)
- {
- struct thread_arg arg;
- from_len = sizeof(from_sin);
- s_client = accept(s, (struct sockaddr *)&from_sin, &from_len);
- int i = 0;
- arg.s_client = s_client;
- //strcpy(arg.buf,buf);
- if (pthread_create(&tid[i++],NULL, calculation, &arg) != 0)
- {
- printf("cannot create thread");
- }
- if(i >= 50)
- {
- i = 0;
- while(i < 50)
- {
- pthread_join(tid[i++], NULL);
- }
- i = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement