|
|
|
@ -11,13 +11,12 @@
|
|
|
|
|
|
|
|
|
|
struct addrinfo *result; /* tableau des adresses réseaux des clients */
|
|
|
|
|
|
|
|
|
|
int cree_socket(struct addrinfo hints, char* port){
|
|
|
|
|
void cree_socket(struct addrinfo hints, char* port){
|
|
|
|
|
int s = getaddrinfo(NULL, port, &hints, &result);
|
|
|
|
|
if (s != 0) {
|
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
|
s = 1;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void con_client(int *sfd){
|
|
|
|
@ -42,38 +41,43 @@ void con_client(int *sfd){
|
|
|
|
|
fprintf(stderr, "Could not connect\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
listen(*sfd, MAX_CON);
|
|
|
|
|
if (listen(*sfd, MAX_CON) == -1){
|
|
|
|
|
fprintf(stderr, "erreur du listen");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
freeaddrinfo(result); /* libère la structure d'adresse client */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void traite_connexion(int sfd, int s){
|
|
|
|
|
void traite_connexion(int *sfd){
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
|
socklen_t peer_addr_len;
|
|
|
|
|
/* Lit les datagrammes et les renvoi au client */
|
|
|
|
|
int s, new_sfd;
|
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
|
|
|
|
|
|
/* Lit les messages et les renvoi au client */
|
|
|
|
|
for (;;) {
|
|
|
|
|
peer_addr_len = sizeof(struct sockaddr_storage);
|
|
|
|
|
accept(sfd, (struct sockaddr*)&peer_addr, &peer_addr_len); /* accepte la connexion */
|
|
|
|
|
nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
|
|
|
new_sfd = accept(*sfd, (struct sockaddr*) &peer_addr, &peer_addr_len); /* accepte la connexion */
|
|
|
|
|
nread = read(new_sfd, buf, BUF_SIZE); /* lit dans le buffer de la connexion, renvoie la taille du message */
|
|
|
|
|
if (nread == -1){
|
|
|
|
|
continue;
|
|
|
|
|
/* ignore la mauvaise requete */
|
|
|
|
|
continue; /* ignore la mauvaise requete */
|
|
|
|
|
}
|
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
|
s = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
|
if (s == 0){
|
|
|
|
|
printf("Recu %ld octets de %s:%s\n", (long) nread, host, service);
|
|
|
|
|
if (write(new_sfd, buf, nread) != nread){
|
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
|
}
|
|
|
|
|
close(new_sfd);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
|
}
|
|
|
|
|
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
|
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
int s; /* s code de retour de getaddrinfo */
|
|
|
|
@ -92,16 +96,10 @@ int main(int argc, char *argv[]){
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM; /* socket en mode datagramme */
|
|
|
|
|
hints.ai_protocol = IPPROTO_TCP; /* protocol de connexion */
|
|
|
|
|
|
|
|
|
|
s = cree_socket(hints, port); /* création du socket */
|
|
|
|
|
if(s == 1) {
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
con_client(&sfd);
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* libère la structure d'adresse client */
|
|
|
|
|
|
|
|
|
|
traite_connexion(sfd, s);
|
|
|
|
|
cree_socket(hints, port); /* création du socket */
|
|
|
|
|
|
|
|
|
|
con_client(&sfd); /* connexion au client */
|
|
|
|
|
|
|
|
|
|
traite_connexion(&sfd);
|
|
|
|
|
printf("traite_connexion ok\n");
|
|
|
|
|
}
|
|
|
|
|