Vous êtes sur la page 1sur 3

#include #include #include #include #include #include #include #include # define

<string.h> <errno.h> <sys/types.h> <sys/socket.h> <stdio.h> <stdlib.h> <unistd.h> "os1ex2part1.h" SOCKET_PATH "mysocket"

// Sinartisi pou dimiourgei to socket me to opoio epikoinonoun controller-client s int connectToServer() { struct sockaddr clntaddr; socklen_t address; //used to keep the socket descriptor(a small integer value similar to file descriptor) that returns the call socket int sock1; //creation of UNIX stream socket for client int sock1=socket(AF_UNIX, SOCK_STREAM, 0); /*i klisi sistim atos socket pernei 3 orismata: to 1o orizei tin oikogeneia stin opoia anikei to socket to 2o ton tipo to u socket kai to 3o to protocolo*/ if ( sock1<0 ) { //check for errors printf("Error opening socket%d\n",errno); //terminates process exit(1); } //dimiourgia tis dieythinsis pou prokeitai na sindethei o c lient //orismos tou domain clntaddr.sa_family=AF_UNIX; //antigrafi toy arxeiou SOCKET_PATH ston pinaka sa_data ti s domis tipou sockaddr me onoma clntaddr strcpy(clntaddr.sa_data,SOCKET_PATH ); //to megethos tis domis tipou sockaddr me onoma clntaddr address=sizeof(clntaddr.sa_family)+strlen(clntaddr.sa_data) ; /*sindesi me tin dieythinsi gia na einai epitixis tha pr epei o server idi na exei kanei bind me tin dieythinsi ayti kai na einai etoimos na akousei tin sindesi xrisimopoiontas tin function listen*/ errno=connect(sock1,&clntaddr,address); if ( errno < 0 ) { //check for errors printf("connection error %d\n", errno); //terminate client process exit(-1); } else

{ //Inform user printf("Connected to server..\n"); } return sock1; }

//sinartisi me tin opoia o client epikoinonei me ton controller kai zitaei prosb asi ston zotiko poro gia anagnosi void requestReadResourse(int sock1, int id) { int type_client=0; //apostoli ston controller twn xaraktiristikwn tou cli ent write(sock1,&id,sizeof(int)); write(sock1,&type_client,sizeof(int)); } //sinartisi me tin opoia o client epikoinonei me ton controller kai zitaei prosb asi ston zotiko poro gia allagi void requestWriteResourse(int sock1, int id) { int type_client=1; //apostoli ston controller twn xaraktiristikwn tou cl ient write(sock1,&id,sizeof(int)); write(sock1,&type_client,sizeof(int)); } //sinartisi me tin opoia o client perimenei eos otou o controller epitrepsei tin prosbasi sto zotiko poro void waitForAccess(int sock1) { int reply; read(sock1,&reply,sizeof(int)); }

/*sinartisi me tin opoia o client,afou oloklirosei tin ektelesei tou krisimou tm imatos, enimeronei ton controller gia na apeleytherothei o poros*/ void releaseResourse(int sock1,int ReleaseRes) { write(sock1,&ReleaseRes,sizeof(int)); } int ReleaseRes; // I kiria sinartisi tou programmatos mas. int main(int argc, char *argv[]) {

/* Klisi tis sinartisis connectToServer() gia tin epikoinonia meta ksi clients kai controller. */ int sock1 = connectToServer(); int id = atoi(argv[3]); if (strcmp(argv[2],"read")==0) { // o client epikoinonei me ton controller kai zitaei pro sbasi ston zotiko poro gia anagnosi requestReadResourse(sock1, id); // o client perimenei eos otou o controller epitrepsei t in prosbasi sto zotiko poro waitForAccess(sock1); //o client exei apoktisei prosbasi ston zotiko poro gia anagnosi kai ektelei to krisimo tmima tou readResource(); ReleaseRes=0; /*o client otan oloklirosei tin ektelesi tou krisimou tm imatos tou enimeronei ton controller gia na apeleytherothei o poros*/ releaseResourse(sock1,ReleaseRes); } else { //o client epikoinonei me ton controller kai zitaei pros basi ston zotiko poro gia allagi requestWriteResourse(sock1, id); //o client perimenei eos otou o controller epitrepsei ti n prosbasi sto zotiko poro waitForAccess(sock1); //o client exei apoktisei prosbasi ston zotiko poro gia anagnosi kai ektelei to krisimo tmima tou writeResource(); ReleaseRes=1; /*o client otan oloklirosei tin ektelesi tou krisimou tm imatos tou enimeronei ton controller gia na apeleytherothei o poros*/ releaseResourse(sock1,ReleaseRes); } }