211 lines
4.6 KiB
C
211 lines
4.6 KiB
C
/* 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 le fork */
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
|
|
/* pour la manipulation des strings */
|
|
#include <string.h>
|
|
|
|
/* Taille du tableau d'int fourni au client */
|
|
#define SIZE_TAB 128
|
|
|
|
#define NB_SEP 65
|
|
#define NB_PAR_LIGNE 16
|
|
|
|
|
|
|
|
//printf("BEFORE: %p\n", __builtin_return_address(0));
|
|
|
|
void handle_sigsegv(int signum)
|
|
{
|
|
fprintf(stderr,"Signal SIGSEGV (%d) received : SEG FAULT\n", signum);
|
|
_exit(0);
|
|
}
|
|
|
|
int min(int a, int b) {
|
|
if (a<b) {
|
|
return a;
|
|
} else {
|
|
return b;
|
|
}
|
|
}
|
|
|
|
// affichage du nombre de connexions
|
|
void print_nb_connexions(int nb_connexions) {
|
|
if (nb_connexions == 1) {
|
|
printf("Nouvelle connexion, 1ere connexion\n");
|
|
} else if (nb_connexions == 2) {
|
|
printf("Nouvelle connexion, 2nd connexion\n");
|
|
} else {
|
|
printf("Nouvelle connexion, %deme connexion\n", nb_connexions);
|
|
}
|
|
printf("L'adresse du nombre de connexions est : %p\n", &nb_connexions);
|
|
}
|
|
|
|
void print_ligne(int * ligne, int n) {
|
|
printf("|");
|
|
int i;
|
|
for (i=0; i<n; i++) {
|
|
printf(" %d |", ligne[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void print_sep() {
|
|
int i;
|
|
for (i=0; i<NB_SEP; i++) {
|
|
printf("-");
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void print_tab(int * tab, int n) {
|
|
print_sep();
|
|
int i;
|
|
for (i=0; i<n; i+=NB_PAR_LIGNE) {
|
|
print_ligne(&(tab[i]), min(n-i, NB_PAR_LIGNE));
|
|
print_sep();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Fonction lancée a chaque fork
|
|
void process(int sock) {
|
|
// Tableau des datas du client
|
|
int tab[SIZE_TAB];
|
|
|
|
// initialisation (tt a 0)
|
|
int i;
|
|
for (i=0; i<SIZE_TAB; i++) {
|
|
tab[i] = 0;
|
|
}
|
|
|
|
int quit = 0;
|
|
|
|
while (quit == 0) {
|
|
// récupération de l'input du client
|
|
char buff[40] = "\0";
|
|
read(sock, buff, 40);
|
|
|
|
char what;
|
|
int param1;
|
|
int param2;
|
|
int param3;
|
|
|
|
sscanf(buff, "%c.%d.%d.%d", &what, ¶m1, ¶m2, ¶m3);
|
|
printf("%c.%d.%d.%d\n", what, param1, param2, param3);
|
|
if (what == 'r') {
|
|
char buff_envoi[40];
|
|
sprintf(buff_envoi, "%d", tab[param1]);
|
|
send(sock, buff_envoi, strlen(buff_envoi), 0);
|
|
} else if (what == 'w') {
|
|
tab[param2] = param1;
|
|
} else if (what == 'a') {
|
|
tab[param1] = tab[param2] + tab[param3];
|
|
} else if (what == 's') {
|
|
tab[param1] = tab[param2] - tab[param3];
|
|
} else if (what == 'm') {
|
|
tab[param1] = tab[param2] * tab[param3];
|
|
} else if (what == 'd') {
|
|
tab[param1] = tab[param2] / tab[param3];
|
|
} else if (what == 'l') {
|
|
if (tab[param1] < tab[param2]) {
|
|
send(sock, "1", 1, 0);
|
|
} else {
|
|
send(sock, "0", 1, 0);
|
|
}
|
|
} else if (what == 'e') {
|
|
if (tab[param1] == tab[param2]) {
|
|
send(sock, "1", 1, 0);
|
|
} else {
|
|
send(sock, "0", 1, 0);
|
|
}
|
|
} else if (what == 'p') {
|
|
print_tab(tab, param1);
|
|
} else {
|
|
quit = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
if (argc != 2) {
|
|
printf("ERREUR : Usage : ./serveur N°Port\n");
|
|
exit(2);
|
|
}
|
|
|
|
// install a handler for SIGSEGV in a part of your code
|
|
if(signal(SIGSEGV,&handle_sigsegv)==SIG_ERR)
|
|
{
|
|
fprintf(stderr,"Could not install SIGSEGV handler");
|
|
return -1;
|
|
}
|
|
|
|
// On crée le socket local
|
|
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 local
|
|
struct sockaddr_in addr_local;
|
|
int binder;
|
|
|
|
addr_local.sin_family = AF_INET;
|
|
addr_local.sin_port = atoi(argv[1]);
|
|
addr_local.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
//On bind l'adresse du socket créee avec le socket local
|
|
binder = bind(sock,(struct sockaddr *) &addr_local, sizeof(struct sockaddr_in));
|
|
if (binder == -1){
|
|
printf("ERREUR lors du bind du socket\n");
|
|
exit(1);
|
|
}
|
|
|
|
//Initialisation file d'attente
|
|
listen(sock, 100);
|
|
|
|
// Variable de stockage de l'addresse emmeteur
|
|
struct sockaddr_in addr_em;
|
|
unsigned int longueur_addr_em = sizeof(struct sockaddr_in);
|
|
|
|
// On se met en état d'acceptation de connexion (et on crée un socket en passant)
|
|
int pid = 1;
|
|
int nb_connexions = 0;
|
|
while (pid != 0){
|
|
|
|
int sock_connexion = accept(sock, (struct sockaddr *)&addr_em, &longueur_addr_em);
|
|
nb_connexions++;
|
|
pid = fork();
|
|
|
|
if (pid == -1){
|
|
printf("ERREUR lors du fork\n");
|
|
exit(1);
|
|
} else if (pid == 0) {
|
|
if (sock_connexion == -1) {
|
|
printf("ERREUR lors de l'acceptation de la connexion\n");
|
|
exit(1);
|
|
} else {
|
|
// Traitement de la connexion
|
|
process(sock_connexion);
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|