PIR/Client.c

174 lines
4.8 KiB
C

/* librairie standard ... */
/* déclaration des types de base */
#include <sys/types.h>
/* constantes relatives aux domaines, types et protocoles */
#include <sys/socket.h>
/* constantes et structures propres au domaine INTERNET */
#include <netinet/in.h>
/* structures retournées par les fonctions de gestion de la base de
données du réseau */
#include <netdb.h>
/* pour les entrées/sorties */
#include <stdio.h>
/* la lib standard */
#include <stdlib.h>
/* pour memcpy */
#include <string.h>
#include <unistd.h>
int main (int argc, char * argv[]) {
if (argc != 3) {
printf("ERREUR : Usage : ./client AdresseServeur N°Port\n");
exit(2);
}
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("ERREUR lors de la création du socket\n");
exit(1);
}
// On cree l'adresse du socket target
struct sockaddr_in addr_target;
addr_target.sin_family = AF_INET;
addr_target.sin_port = atoi(argv[2]);
struct hostent * hoststruct = gethostbyname(argv[1]);
if (hoststruct == NULL){
printf("ERREUR lors de la recherche de l'adresse IP du target\n");
exit(1);
}
memcpy((char *)&(addr_target.sin_addr.s_addr), hoststruct->h_addr, hoststruct->h_length);
int retour_connexion;
//On fait de demande de connextion au socket target
retour_connexion = connect(sock, (struct sockaddr *)&addr_target, sizeof(struct sockaddr_in));
if (retour_connexion == -1){
printf("ERREUR lors de la demande de connexion\n");
exit(1);
}
printf("Que souhaitez vous faire : \n - READ 'index'\n - WRITE 'value' 'index'\n - ADD 'index1' 'index2' 'index3'\n - SUB 'index1' 'index2' 'index3'\n - MUL 'index1' 'index2' 'index3'\n - DIV 'index1' 'index2' 'index3'\n - LOWER 'index1' 'index2'\n - EQUAL 'index1' 'index2'\n - PRINT 'size'\n - QUIT\n");
int cont = 1;
while (cont) {
// Lecture de la requette
char requette[40];
fgets(requette, 40, stdin);
char req[10] = "\0";
int param1 = -1;
int param2 = -1;
int param3 = -1;
sscanf(requette, "%s %d %d %d", req, &param1, &param2, &param3);
char buff[40] = "\0";
if (!strcmp(req, "READ")) {
if (param1 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "r.%d.0.0", param1);
send(sock, buff, strlen(buff), 0);
read(sock, buff, 40);
printf("Tableau[%d] = %s\n", param1, buff);
}
} else if (!strcmp(req, "WRITE")) {
if (param2 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "w.%d.%d.0", param1, param2);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "ADD")) {
if (param1 < 0 || param2 < 0 || param3 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "a.%d.%d.%d", param1, param2, param3);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "SUB")) {
if (param1 < 0 || param2 < 0 || param3 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "s.%d.%d.%d", param1, param2, param3);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "MUL")) {
if (param1 < 0 || param2 < 0 || param3 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "m.%d.%d.%d", param1, param2, param3);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "DIV")) {
if (param1 < 0 || param2 < 0 || param3 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "d.%d.%d.%d", param1, param2, param3);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "LOWER")) {
if (param1 < 0 || param2 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "l.%d.%d.0", param1, param2);
send(sock, buff, strlen(buff), 0);
char retour[2];
read(sock, retour, 2);
char res[6];
if (!strcmp(retour, "1")) {
strcpy(res, "TRUE");
} else if (!strcmp(retour, "0")) {
strcpy(res, "FALSE");
} else {
strcpy(res, "ERROR");
}
printf("Tableau[%d] < Tableau[%d] => %s\n", param1, param2, res);
}
} else if (!strcmp(req, "EQUAL")) {
if (param1 < 0 || param2 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "e.%d.%d.0", param1, param2);
send(sock, buff, strlen(buff), 0);
char retour[2];
read(sock, retour, 2);
char res[6];
if (!strcmp(retour, "1")) {
strcpy(res, "TRUE");
} else if (!strcmp(retour, "0")) {
strcpy(res, "FALSE");
} else {
strcpy(res, "ERROR");
}
printf("Tableau[%d] == Tableau[%d] => %s\n", param1, param2, res);
}
} else if (!strcmp(req, "PRINT")) {
if (param1 < 0) {
printf("ERROR SYNTAX\n");
} else {
sprintf(buff, "p.%d.0.0", param1);
send(sock, buff, strlen(buff), 0);
}
} else if (!strcmp(req, "QUIT")) {
sprintf(buff, "q.0.0.0");
send(sock, buff, strlen(buff), 0);
cont = 0;
} else {
printf("SYNTAX ERROR\n");
}
}
close(sock);
return 0;
}