sa march bitch
This commit is contained in:
parent
8558a4f5e4
commit
caae36ed9a
21
RPC/server.c
21
RPC/server.c
|
@ -5,12 +5,20 @@ 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){
|
||||
|
||||
case FIN:
|
||||
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
39
client.c
39
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);
|
||||
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));
|
||||
|
||||
/* (2) Check if send succesfully */
|
||||
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;
|
||||
}
|
||||
|
|
9
makefile
9
makefile
|
@ -1,5 +1,7 @@
|
|||
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,11 +21,12 @@ 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
|
||||
serverRPC.o: RPC/server.c RPC/server.h
|
||||
gcc $(CC) -c -o serverRPC.o RPC/server.c
|
||||
|
||||
|
||||
|
||||
|
||||
all: client server
|
||||
|
||||
clean:
|
||||
|
|
43
server.c
43
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");
|
||||
|
|
1
server.h
1
server.h
|
@ -15,6 +15,7 @@
|
|||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include "RPC/server.h"
|
||||
|
||||
|
||||
#include "lib.h"
|
||||
|
|
Loading…
Reference in New Issue