sa march bitch

This commit is contained in:
MASCARO LUCAS 2017-02-22 12:35:41 +01:00
parent 8558a4f5e4
commit caae36ed9a
9 changed files with 59 additions and 64 deletions

View File

@ -5,11 +5,19 @@ void traiterClient(int socket){
requete req; requete req;
int nb_octet; int nb_octet;
int fini=0; int fini=0;
char i = 0;
while(!fini){ while(!fini){
nb_octet = read(socket,&req,sizeof(req)); nb_octet = read(socket,&req,sizeof(requete));
IF_NB_OCTET_BREAK 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){ switch(req.type){
@ -25,12 +33,19 @@ void traiterClient(int socket){
int nb; int nb;
long res; 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 IF_NB_OCTET_BREAK
DEBUG&& printf("%d\n",nb);
res = factoriel(nb); res = factoriel(nb);
DEBUG&& debug("loop","Sending result");
nb_octet = write(socket, &res, sizeof(res)); nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK IF_NB_OCTET_BREAK
DEBUG&& printf("done\n");
break; break;
} }
@ -71,7 +86,7 @@ void traiterClient(int socket){
attendu -= nb_octet; attendu -= nb_octet;
} }
analyse(donnees,nb_elem,&res); analyser_donnees(donnees,nb_elem,&res);
write(socket, &res, sizeof(res)); write(socket, &res, sizeof(res));
free (donnees); free (donnees);

View File

@ -8,7 +8,7 @@
#include "../calculs.h" #include "../calculs.h"
#include "../lib.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 #define MAX_BUFFER_LENGTH 200
void traiterClient(int socket); void traiterClient(int socket);

View File

@ -14,8 +14,10 @@ int main(int argc, char* argv[]){
/* (2) Misc. information */ /* (2) Misc. information */
int bytes; // transfer count int bytes; // transfer count
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); char* to_send = (char*) malloc( sizeof(requete)+sizeof(int)+1 );
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); long to_recv;
requete request;
int param;
/* [2] Manage arguments /* [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); sock = xconnect(hostname, port, &server_addr);
@ -62,25 +64,33 @@ int main(int argc, char* argv[]){
} }
/* [4] Send message request.type = FACTO;
=========================================================*/ //size of the request isn't important
/* (1) Send message to server */ request.taille = sizeof(int);
bytes = xwrite(sock, to_send);
param = 2;
/* (2) Check if send succesfully */
memcpy(to_send,&request,sizeof(requete));
memcpy(to_send+sizeof(requete),&param,sizeof(int));
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
sleep(2);
param = 3;
memcpy(to_send+sizeof(requete),&param,sizeof(int));
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
if( bytes == -1 ){ if( bytes == -1 ){
perror("erreur envoi message"); perror("erreur envoi requete");
exit(1); exit(1);
} }
DEBUG&& printf("*** sent %d bytes\n", bytes);
/* [5] Wait for response /* [5] Wait for response
=========================================================*/ =========================================================*/
/* (1) 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) */ /* (2) Check if received successfully (and consistently) */
if( bytes == -1 ){ if( bytes == -1 ){
@ -89,12 +99,13 @@ int main(int argc, char* argv[]){
} }
/* (4) log */ /* (4) log */
printf("*** received : '%s'\n", to_recv); printf("*** received : '%ld'\n", to_recv);
/* [6] Close socket /* [6] Close socket
=========================================================*/ =========================================================*/
close(sock); close(sock);
printf("Closing socket\n");
return 0; return 0;
} }

BIN
client.o

Binary file not shown.

BIN
lib.o

Binary file not shown.

View File

@ -1,6 +1,8 @@
CC=-Werror -g -pthread CC=-Werror -g -pthread
calculs.o: calculs.c calculs.h
gcc $(CC) -c -o calculs.o calculs.c
client.o: client.h client.c client.o: client.h client.c
gcc $(CC) -c -o client.o 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 lib.o: lib.h lib.c
gcc $(CC) -c -o lib.o lib.c gcc $(CC) -c -o lib.o lib.c
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 gcc $(CC) -o server lib.o server.o serverRPC.o calculs.o
client: lib.o client.o client: lib.o client.o
gcc $(CC) -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 clientRPC.o: calculs.h RPC/client.c
gcc $(CC) -c -o clientRPC.o RPC/client.c gcc $(CC) -c -o clientRPC.o RPC/client.c
serverRPC.o: RPC/server.c RPC/server.h calculs.h serverRPC.o: RPC/server.c RPC/server.h
gcc $(CC) -c -o serverRPC.o RPC/server.c gcc $(CC) -c -o serverRPC.o RPC/server.c

View File

@ -127,8 +127,6 @@ void* manageClient(void* pthread_args){
=========================================================*/ =========================================================*/
/* (1) Initialize socket data */ /* (1) Initialize socket data */
struct sockaddr_in addr_client; 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 int bytes; // transfer count
char client_ip[20]; char client_ip[20];
@ -139,45 +137,12 @@ void* manageClient(void* pthread_args){
int datasock = arguments->socket; int datasock = arguments->socket;
int thread_index = arguments->index; 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"); /* [2] End process
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
=========================================================*/ =========================================================*/
/* (1) Close data socket */ /* (1) Close data socket */
DEBUG&& debug("thread", "closing socket"); DEBUG&& debug("thread", "closing socket");

View File

@ -15,6 +15,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
#include "RPC/server.h"
#include "lib.h" #include "lib.h"

BIN
server.o

Binary file not shown.