fonction traite_connexion

tcp
Flavien Haas 5 years ago
parent 8aecd5e4a8
commit d52563a834

BIN
.DS_Store vendored

Binary file not shown.

@ -3,8 +3,8 @@ all: clean serveur client
clean: clean:
rm -f *.o rm -f *.o
serveur: serveur: serveur_echo.c
gcc -c serveur_echo.c -o serveur_echo.o gcc -c serveur_echo.c -o serveur_echo.o
client: client: client_echo.c
gcc -c client_echo.c -o client_echo.o gcc -c client_echo.c -o client_echo.o

@ -11,13 +11,12 @@
struct addrinfo *result; /* tableau des adresses réseaux des clients */ 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); int s = getaddrinfo(NULL, port, &hints, &result);
if (s != 0) { if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
s = 1; exit(EXIT_FAILURE);
} }
return s;
} }
void con_client(int *sfd){ void con_client(int *sfd){
@ -42,38 +41,43 @@ void con_client(int *sfd){
fprintf(stderr, "Could not connect\n"); fprintf(stderr, "Could not connect\n");
exit(EXIT_FAILURE); 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; ssize_t nread;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
struct sockaddr_storage peer_addr; struct sockaddr_storage peer_addr;
socklen_t peer_addr_len; 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 (;;) { for (;;) {
peer_addr_len = sizeof(struct sockaddr_storage); peer_addr_len = sizeof(struct sockaddr_storage);
accept(sfd, (struct sockaddr*)&peer_addr, &peer_addr_len); /* accepte la connexion */ new_sfd = 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); nread = read(new_sfd, buf, BUF_SIZE); /* lit dans le buffer de la connexion, renvoie la taille du message */
if (nread == -1){ if (nread == -1){
continue; continue; /* ignore la mauvaise requete */
/* 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); s = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);
if (s == 0){ if (s == 0){
printf("Recu %ld octets de %s:%s\n", (long) nread, host, service); 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{ else{
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); 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[]){ int main(int argc, char *argv[]){
struct addrinfo hints; struct addrinfo hints;
int s; /* s code de retour de getaddrinfo */ 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_socktype = SOCK_STREAM; /* socket en mode datagramme */
hints.ai_protocol = IPPROTO_TCP; /* protocol de connexion */ hints.ai_protocol = IPPROTO_TCP; /* protocol de connexion */
s = cree_socket(hints, port); /* création du socket */ 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);
con_client(&sfd); /* connexion au client */
traite_connexion(&sfd);
printf("traite_connexion ok\n");
} }

Loading…
Cancel
Save