Читайте также:
|
|
Клиент
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/sem.h>
#include <unistd.h>
char message[512];
char buf[sizeof(message)];
/// CLIENT
using namespace std;
int main()
{
int sock;
sockaddr srvr_name;
char buf[512];
int bytes_read;
puts("Input program code:");
gets(message);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket");
return 0;
}
srvr_name.sa_family = AF_UNIX;
strcpy(srvr_name.sa_data, "socket.soc");
if(connect(sock, &srvr_name, sizeof(srvr_name)) < 0)
{
perror("connect");
return 0;
}
send(sock, message, sizeof(message), 0);
recv(sock, buf, sizeof(message), 0);
puts(buf);
close(sock);
return 0;
}
Сервер
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/sem.h>
#include <unistd.h>
#include <stdlib.h>
int sockets[40];
pthread_t threads[10];
pthread_mutex_t mutexs[10];
bool statesthread[10];
int sockets_index[10];
static void *funct(void *arg)
{
char *chr;
chr=(char*)arg;
puts(chr);
int k=atoi(chr);
while(true)
{
pthread_mutex_init(&mutexs[k],NULL);
while(1)
{
char s[512];
int bytes_read = recv(sockets[sockets_index[k]], s, 512, 0);
if(bytes_read <= 0) break;
puts(s);
bool flag=true,dot=true;
int state=0;
int i=0;
int errCount=0;
bool state1, //имеет название
state2, //открыта (
state3, //закрыта)
state4, //открыта '
state5, //закрыта '
state6, //открыта "
state7, //закрыта "
state8; //
while (i<strlen(s))
{
state1 = false;
state2 = false;
state3 = false;
state4 = false;
state5 = false;
state6 = false;
state7 = false;
while (i<strlen(s) & s[i]!=';')
{
if(errCount==0)
switch(s[i])
{
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'w':
case 'x':
case 'y':
case 'z':
if(state1==true & state2==true & state3==true)errCount++;
else
{
if(state1==true & state2==true & state4==true & state8==true)errCount++;
if(state1==false)state1=true;
if(state4==true)state8=true;
}
break;
case '\'':
if(state1==true & state2==true & state4==true & state8==true)state5=true;
else errCount++;
break;
case '\"':
if(state1==true & state2==true & state6==false)state6=true;
if(state1==true & state2==true & state6==true &state7==false)state7=true;
if(state6==true & state7==true)errCount++;
break;
case '(':
if(state1==true & state2==false & state3==false & state4==false & state6==false)state2=true;
else errCount++;
break;
case ')':
if(state1==true & state2==true & state3==false)
{
state3=true;
break;
}
if(state1==true & state2==false)errCount++;
if(state1==true & state2==true & state3==true & state4==false & state6==false)errCount++;
break;
case ',':
if(state1==false | state2==false & state3==false)errCount++;
if(state1==true & state2==true & state3==true)errCount++;
if(state1==true & state2==true & state3==false & state4==true & state5==false & state8==false)state8==false;
if(state1==true & state2==true & state3==false & state4==true & state5==false & state8==true)errCount;
break;
default:
break;
}
i++;
}
i++;
}
if(state1==false | state2==false | state3==false)errCount++;
if(errCount>0){send(sockets[sockets_index[k]], "Errors found", strlen("Errors found"), 0);}
else {send(sockets[sockets_index[k]], "Errors not found", strlen("Errors not found"), 0);}
close(sockets[sockets_index[k]]);
sockets[sockets_index[k]]=-1;
pthread_mutex_unlock(&mutexs[k]);
break;
}
}
}
static void *ini(void *arg)
{
for(int j=0; j<10; j++)
{
pthread_mutex_init(&mutexs[j],NULL);
}
}
int main()
{
pthread_t initial;
pthread_create(&initial,NULL, ini, NULL);
pthread_join(initial,NULL);
char *c1=new char[2];
for(int j=0; j<10; j++)
{
c1=new char[2];
sprintf(c1,"%d",j);
pthread_mutex_lock(&mutexs[j]);
pthread_create(&threads[j],NULL, funct, c1);
statesthread[j]=false;
}
int sock, listener;
sockaddr srvr_name;
int bytes_read;
memset(sockets,-1,sizeof(sockets));
listener = socket(AF_UNIX, SOCK_STREAM, 0);
if(listener < 0)
{
perror("socket");
return 0;
}
srvr_name.sa_family = AF_UNIX;
strcpy(srvr_name.sa_data, "socket.soc");
if(bind(listener, &srvr_name, sizeof(srvr_name)) < 0)
{
perror("bind");
return 0;
}
listen(listener, 40);
pthread_t fun;
fflush(stdin);
bool flag=true;
bool flag2;
int i,k,k2;
while(1)
{
flag=true;
for(i=0; i<sizeof(sockets)&&flag==true; i++)
{
if(sockets[i]==-1)
{
flag=false;
k=i;
}
}
if(flag==false)
{
sockets[k] = accept(listener, NULL, NULL);
if(sockets[k] < 0)
{
perror("accept");
return 0;
}
flag2=true;
for(int j=0; j<10&&flag2==true; j++)
{
if(statesthread[j]==false)
{
statesthread[j]=true;
flag2=false;
k2=j;
}
}
if(flag2==false)
{
sockets_index[k2]=k;
pthread_mutex_unlock(&mutexs[k2]);
}
}
}
return 0;
}
Вывод: В ходе выполнения данной лабораторной работы была разработана многопоточное приложение, выполняющее решение поставленной задачи.
Дата добавления: 2015-12-07; просмотров: 60 | Нарушение авторских прав