Old stuff/ecole_etude_fac_de_pau/licence_3/projet_commun_se/backup16/client.c
(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <rpc/xdr.h>
#include "include/tcp.h"
#include "include/global.h"
#include "include/demandes.h"
#include "include/sockets.h"
int main(int argc, char** argv)
{
sock = con_tcp(4000,"localhost");
// printf("Client %d\n",sock);
fd_set sockets;
int nb = 0;
nb_clients = &nb;
pid_t pid;
if((pid = fork()) == -1) { perror("client fork error"); exit(-1); }
if (pid == 0)
{
while(1)
{
select_client(&sockets);
if (FD_ISSET(sock,&sockets))
{
XDR reception;
retour_item ret;
reception.x_op=XDR_DECODE;
printf("Socket de lecture %d\n",sock);
if (check_fermeture_client(sock))
{
exit(-1);
}
xdrrec_create(&reception, 0, 0, (void*)sock, (void*)read_tcp, (void*)write_tcp);
if (!xdrrec_skiprecord(&reception)) { perror("client.c skiprecord"); exit(-1); }
if (!xdr_retour(&reception,&ret)) { perror("clients.c xdr_demande"); exit(-1); }
printf("Recu le retour %d %d %d %d\n",ret.demi_blonde, ret.demi_brune, ret.peinte_blonde, ret.peinte_brune);
}
}
}
else
{
demande_item dem;
XDR data;
data.x_op=XDR_ENCODE;
xdrrec_create(&data, 0, 0, (void*)sock, (void*)read_tcp, (void*)write_tcp);
int op=1;
while (op != 0)
{
bzero(&dem,sizeof(demande_item));
printf(" ***********\n\t1- Demande de bieres\n\t2- Tournee generale\n\t0- Quitter\n*************\n\n");
/*
fflush(stdin);
scanf("%d",&op);
if(op == 1)
{
int ajout=1;
while(ajout != 0)
{
printf("1- Ajout d une demi brune\n\t2- Ajout d une demi blonde\n\t3- Ajout d une peinte brune\n\t4- Ajout d une peinte blonde\n\t0- fin\n");
scanf("Entrez votre choix: %d",&ajout);
switch(ajout)
{
case 1: dem.demi_brune++; break;
case 2: dem.demi_blonde++; break;
case 3: dem.peinte_brune++; break;
case 4: dem.peinte_blonde++; break;
default: printf("Choix non valide\n");
}
}
}
*/
dem.demi_brune=1;
dem.peinte_blonde=1;
if (!xdr_int(&data,&op)) { perror("clients.c xdr_int"); exit(-1); }
if (!xdr_demande(&data,&dem)) { perror("clients.c xdr_demande"); exit(-1); }
if (!xdrrec_endofrecord(&data, TRUE)) { perror("send eof"); exit(-1); }
printf("ENVOIE de la demande\n");
sleep(20);
printf("Suite ...\n");
}
xdr_destroy(&data);
}
close(sock);
return 0;
}