diff --git a/RPC/server.c b/RPC/server.c index 6d8350d..df12c1b 100644 --- a/RPC/server.c +++ b/RPC/server.c @@ -5,11 +5,19 @@ void traiterClient(int socket){ requete req; int nb_octet; int fini=0; + char i = 0; + while(!fini){ - - nb_octet = read(socket,&req,sizeof(req)); + + nb_octet = read(socket,&req,sizeof(requete)); IF_NB_OCTET_BREAK + + if(nb_octet == 0){ + break; + } + + DEBUG&& printf("Request received (operation/size) : (%d/%d)\n",req.type,req.taille); switch(req.type){ @@ -25,12 +33,19 @@ void traiterClient(int socket){ int nb; long res; - nb_octet = read(socket, &nb, sizeof(int)); + DEBUG&& debug("loop","FACTO asked"); + + nb_octet = read(socket,&nb,sizeof(int)); IF_NB_OCTET_BREAK + DEBUG&& printf("%d\n",nb); + res = factoriel(nb); + DEBUG&& debug("loop","Sending result"); nb_octet = write(socket, &res, sizeof(res)); IF_NB_OCTET_BREAK + DEBUG&& printf("done\n"); + break; } @@ -71,7 +86,7 @@ void traiterClient(int socket){ attendu -= nb_octet; } - analyse(donnees,nb_elem,&res); + analyser_donnees(donnees,nb_elem,&res); write(socket, &res, sizeof(res)); free (donnees); diff --git a/RPC/server.h b/RPC/server.h index abadb6f..1a3d5b9 100644 --- a/RPC/server.h +++ b/RPC/server.h @@ -8,7 +8,7 @@ #include "../calculs.h" #include "../lib.h" - #define IF_NB_OCTET_BREAK if(nb_octet == -1){fini = 1; break;} + #define IF_NB_OCTET_BREAK if(nb_octet == -1){DEBUG&& debug("loop","Breaking the loop\n");fini = 1; break;} #define MAX_BUFFER_LENGTH 200 void traiterClient(int socket); diff --git a/client.c b/client.c index 85de852..6bbf5a6 100644 --- a/client.c +++ b/client.c @@ -14,8 +14,10 @@ int main(int argc, char* argv[]){ /* (2) Misc. information */ int bytes; // transfer count - char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); - char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); + char* to_send = (char*) malloc( sizeof(requete)+sizeof(int)+1 ); + long to_recv; + requete request; + int param; /* [2] Manage arguments @@ -51,7 +53,7 @@ int main(int argc, char* argv[]){ - /* [3] Create UPD socket and get target data + /* [3] Create TCP socket and get target data =========================================================*/ sock = xconnect(hostname, port, &server_addr); @@ -62,25 +64,33 @@ int main(int argc, char* argv[]){ } - /* [4] Send message - =========================================================*/ - /* (1) Send message to server */ - bytes = xwrite(sock, to_send); - - /* (2) Check if send succesfully */ + request.type = FACTO; + //size of the request isn't important + request.taille = sizeof(int); + + param = 2; + + memcpy(to_send,&request,sizeof(requete)); + memcpy(to_send+sizeof(requete),¶m,sizeof(int)); + + bytes = write(sock,to_send,sizeof(requete)+sizeof(int)); + + sleep(2); + param = 3; + memcpy(to_send+sizeof(requete),¶m,sizeof(int)); + + bytes = write(sock,to_send,sizeof(requete)+sizeof(int)); + if( bytes == -1 ){ - perror("erreur envoi message"); + perror("erreur envoi requete"); exit(1); } - DEBUG&& printf("*** sent %d bytes\n", bytes); - - /* [5] Wait for response =========================================================*/ /* (1) Wait for response */ - bytes = xread(sock, to_recv, BUFSIZE); + bytes = recv(sock, &to_recv, sizeof(long),0); /* (2) Check if received successfully (and consistently) */ if( bytes == -1 ){ @@ -89,12 +99,13 @@ int main(int argc, char* argv[]){ } /* (4) log */ - printf("*** received : '%s'\n", to_recv); + printf("*** received : '%ld'\n", to_recv); /* [6] Close socket =========================================================*/ close(sock); + printf("Closing socket\n"); return 0; } diff --git a/client.o b/client.o index 9239c85..c0bf93c 100644 Binary files a/client.o and b/client.o differ diff --git a/lib.o b/lib.o index d631430..9380437 100644 Binary files a/lib.o and b/lib.o differ diff --git a/makefile b/makefile index 4181afe..f42cc37 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,8 @@ CC=-Werror -g -pthread - +calculs.o: calculs.c calculs.h + gcc $(CC) -c -o calculs.o calculs.c + client.o: client.h client.c gcc $(CC) -c -o client.o client.c @@ -10,8 +12,8 @@ server.o: lib.h server.h server.c lib.o: lib.h lib.c gcc $(CC) -c -o lib.o lib.c -server: lib.o server.o serverRPC.o - gcc $(CC) -o server lib.o server.o serverRPC.o +server: lib.o server.o serverRPC.o calculs.o + gcc $(CC) -o server lib.o server.o serverRPC.o calculs.o client: lib.o client.o gcc $(CC) -o client lib.o client.o @@ -19,8 +21,9 @@ client: lib.o client.o clientRPC.o: calculs.h RPC/client.c gcc $(CC) -c -o clientRPC.o RPC/client.c -serverRPC.o: RPC/server.c RPC/server.h calculs.h - gcc $(CC) -c -o serverRPC.o RPC/server.c +serverRPC.o: RPC/server.c RPC/server.h + gcc $(CC) -c -o serverRPC.o RPC/server.c + diff --git a/server.c b/server.c index 41a8664..ef711b3 100644 --- a/server.c +++ b/server.c @@ -127,8 +127,6 @@ void* manageClient(void* pthread_args){ =========================================================*/ /* (1) Initialize socket data */ struct sockaddr_in addr_client; - char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); - char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); int bytes; // transfer count char client_ip[20]; @@ -139,45 +137,12 @@ void* manageClient(void* pthread_args){ int datasock = arguments->socket; int thread_index = arguments->index; - strcpy(to_send, "server default response"); + DEBUG&& printf("starting the computation loop\n"); + //Compute client request + traiterClient(datasock); - DEBUG&& debug("thread", "starting thread"); - DEBUG&& printf("success[%d]\n", thread_index); - - /* [2] Wait for data - =========================================================*/ - /* (1) wait/read data */ - bytes = xread(datasock, to_recv, BUFSIZE); - - /* (2) Manage error */ - if( bytes == -1 ){ - perror("erreur reception paquet"); - exit(1); - } - - - // Get client ip - inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20); - - printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port)); - - - - /* [3] Send response - =========================================================*/ - /* (1) Send response */ - bytes = xwrite(datasock, to_send); - - /* (2) Manage error */ - if( bytes == -1 ){ - perror("erreur envoi rĂ©ponse"); - exit(1); - } - - - - /* [4] End process + /* [2] End process =========================================================*/ /* (1) Close data socket */ DEBUG&& debug("thread", "closing socket"); diff --git a/server.h b/server.h index 1dcad0f..69978ed 100644 --- a/server.h +++ b/server.h @@ -15,6 +15,7 @@ #include #include #include + #include "RPC/server.h" #include "lib.h" diff --git a/server.o b/server.o index 390cfc6..69bcd33 100644 Binary files a/server.o and b/server.o differ