traduction du programme serveur

master
Flavien Haas 5 years ago
parent 0b7e2ac5e9
commit 0b53da0e18

@ -8,84 +8,77 @@
#define BUF_SIZE 500
int
main(int argc, char *argv[])
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
ssize_t nread;
char buf[BUF_SIZE];
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(EXIT_FAILURE);
int main(int argc, char *argv[]){
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
ssize_t nread;
char buf[BUF_SIZE];
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(EXIT_FAILURE);
}
//remplissage de la structure hints contenants la configuration réseau du serveur
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6; // utilisation du serveur sur IPv6 (AF_UNSPEC pour IPv4/6)
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
s = getaddrinfo(NULL, argv[1], &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo () retourne une liste de structures d'adresses.
Essayez chaque adresse jusqu'à ce que nous ayons réussi à bind(2).
Si socket() (ou bind()) échoue, nous (fermons le socket et)
essayons l'adresse suivante */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1){
continue;
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, argv[1], &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0){
break; /* Succès */
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind(2).
If socket() (or bind()) fails, we (close the socket
and) try the next address. */
close(sfd);
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == -1)
continue;
if (rp == NULL) { /* Aucune adresse n'a pu marcher */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
}
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
freeaddrinfo(result); /* libère le buffer */
close(sfd);
}
/* Lit les datagrammes et les renvois au client */
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
for (;;) {
peer_addr_len = sizeof(struct sockaddr_storage);
nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);
if (nread == -1){
continue; /* ignore la mauvaise requete */
}
freeaddrinfo(result); /* No longer needed */
/* Read datagrams and echo them back to sender */
for (;;) {
peer_addr_len = sizeof(struct sockaddr_storage);
nread = recvfrom(sfd, buf, BUF_SIZE, 0,
(struct sockaddr *) &peer_addr, &peer_addr_len);
if (nread == -1)
continue; /* Ignore failed request */
char host[NI_MAXHOST], service[NI_MAXSERV];
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("Received %ld bytes from %s:%s\n",
(long) nread, host, service);
else
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
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);
}
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, "Error sending response\n");
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
fprintf(stderr, "Erreur d'envoi de la reponse\n");
}
}
}

Loading…
Cancel
Save