|
|
@ -19,9 +19,32 @@ int cree_socket(char* ip, char* port, struct addrinfo hints){
|
|
|
|
return s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void con_serveur(int *sfd){
|
|
|
|
|
|
|
|
struct addrinfo *rp; // structure de l'adresse du client
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
|
|
|
|
// socket crée un point de communication et renvoie un descripteur , sfd = socket file descriptor
|
|
|
|
|
|
|
|
*sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
|
|
|
|
if (*sfd == -1){
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(*sfd, rp->ai_addr, rp->ai_addrlen) != 1){
|
|
|
|
|
|
|
|
break; // Succès
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close(*sfd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rp == NULL) { // Aucune adresse n'a pu marcher
|
|
|
|
|
|
|
|
fprintf(stderr, "Could not connect\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *rp;
|
|
|
|
|
|
|
|
int sfd, s, j;
|
|
|
|
int sfd, s, j;
|
|
|
|
size_t len;
|
|
|
|
size_t len;
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t nread;
|
|
|
@ -48,26 +71,7 @@ int main(int argc, char *argv[]) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getaddrinfo () retourne une liste de structures d'adresses.
|
|
|
|
con_serveur(&sfd);
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1){
|
|
|
|
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
|
|
|
|
fprintf(stderr, "Could not connect\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
|
|
|
|
|
|
|
|