sysdis-tp/RPC/server.c

85 lines
1.4 KiB
C
Raw Normal View History

2017-02-14 08:31:49 +00:00
#include "server.h"
void traiterClient(int socket){
requete req;
int nb_octet;
int fini=0;
while(!fini){
nb_octet = read(socket,&req,sizeof(req));
IF_NB_OCTET_BREAK
switch(req.type){
case FIN:
{
fini = 1;
break;
}
case FACTO:
{
int nb;
long res;
nb_octet = read(socket, &nb, sizeof(int));
IF_NB_OCTET_BREAK
res = factoriel(nb);
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
break;
}
case POW:
{
int a,b;
long res;
nb_octet = read(socket, &a, sizeof(int));
IF_NB_OCTET_BREAK
nb_octet = read(socket, &b, sizeof(int));
IF_NB_OCTET_BREAK
res = puissance(a,b);
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
break;
}
case ANALYSE:
{
int nb_elem, attendu;
int* donnees;
char message[MAX_BUFFER_LENGTH];
res_analyse_donnees res;
nb_elem = req.taille/sizeof(int);
donnees = (int*) malloc(nb_elem*sizeof(int));
attendu = req.taille;
while(attendu > 0){
nb_octet = read(socket, message, MAX_BUFFER_LENGTH);
IF_NB_OCTET_BREAK
memcpy(donnees+(req.taille - attendu), message,nb_octet);
attendu -= nb_octet;
}
analyse(donnees,nb_elem,&res);
write(socket, &res, sizeof(res));
free (donnees);
break;
}
}
}
}