2019-10-28 17:31:10 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2019-09-23 14:37:16 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2019-10-28 17:31:10 +01:00
|
|
|
#include <string.h>
|
2019-09-23 14:37:16 +02:00
|
|
|
|
2019-10-28 17:31:10 +01:00
|
|
|
#define BUF_SIZE 500
|
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
struct addrinfo *result; /* tableau des adresses réseaux des serveurs */
|
2019-10-29 15:08:21 +01:00
|
|
|
|
|
|
|
int cree_socket(char* ip, char* port, struct addrinfo hints){
|
|
|
|
int s = getaddrinfo(ip, port, &hints, &result);
|
|
|
|
if (s != 0) {
|
2019-10-31 19:18:02 +01:00
|
|
|
fprintf(stderr, "erreur sur l'addresse serveur %s\n", gai_strerror(s));
|
2019-10-29 15:08:21 +01:00
|
|
|
s = 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2019-10-28 17:31:10 +01:00
|
|
|
|
2019-10-29 15:26:03 +01:00
|
|
|
void con_serveur(int *sfd){
|
2019-10-31 19:18:02 +01:00
|
|
|
struct addrinfo *rp; /* structure de l'adresse du client */
|
2019-10-29 15:26:03 +01:00
|
|
|
/* getaddrinfo () retourne une liste de structures d'adresses.
|
2019-10-30 13:13:10 +01:00
|
|
|
Essayez chaque adresse jusqu'à ce que nous ayons réussi à connect(2).
|
|
|
|
Si socket() (ou connect()) échoue, nous (fermons le socket et)
|
2019-10-29 15:26:03 +01:00
|
|
|
essayons l'adresse suivante */
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
2019-10-31 19:18:02 +01:00
|
|
|
/* socket crée un point de communication et renvoie un descripteur , sfd = socket file descriptor */
|
2019-10-29 15:26:03 +01:00
|
|
|
*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){
|
2019-10-31 19:18:02 +01:00
|
|
|
break; /* Succès */
|
2019-10-29 15:26:03 +01:00
|
|
|
}
|
|
|
|
close(*sfd);
|
|
|
|
}
|
2019-10-31 19:18:02 +01:00
|
|
|
if (rp == NULL) { /* Aucune adresse n'a pu marcher */
|
|
|
|
fprintf(stderr, "N'a pu se connecter");
|
2019-10-29 15:26:03 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 08:24:54 +01:00
|
|
|
void sendreceive(char* argtemp, size_t len, int j, int sfd){
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
ssize_t nread;
|
2019-10-31 19:18:02 +01:00
|
|
|
/* Envoie les derniers arguments et lit la réponse du serveur */
|
2019-10-30 08:24:54 +01:00
|
|
|
if (len + 1 > BUF_SIZE) {
|
2019-10-31 19:18:02 +01:00
|
|
|
fprintf(stderr, "Long message ignoré %d\n", j);
|
2019-10-30 08:24:54 +01:00
|
|
|
}
|
|
|
|
if (write(sfd, argtemp, len) != len) {
|
2019-10-31 19:18:02 +01:00
|
|
|
fprintf(stderr, "écriture partiel ou échue\n");
|
2019-10-30 08:24:54 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
nread = read(sfd, buf, BUF_SIZE);
|
|
|
|
if (nread == -1) {
|
|
|
|
perror("read");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-10-31 19:18:02 +01:00
|
|
|
printf("Recu %ld octets: %s\n", (long) nread, buf);
|
2019-10-30 08:24:54 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:08:21 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct addrinfo hints;
|
|
|
|
int sfd, s, j;
|
|
|
|
size_t len;
|
2019-10-30 08:24:54 +01:00
|
|
|
char* argtemp;
|
2019-10-29 15:08:21 +01:00
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
if (argc < 3) { /* vérification du nombre d'arguments rentrés */
|
2019-10-29 15:08:21 +01:00
|
|
|
fprintf(stderr, "mettre: %s ip port message\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* ip = argv[1];
|
|
|
|
char* port = argv[2];
|
2019-10-31 19:18:02 +01:00
|
|
|
char* message = argv[3]; /* remplissage des varables a partir des arguments passés au programme
|
2019-10-29 15:08:21 +01:00
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
/* Obtient des adresses avec hote:port */
|
2019-10-29 15:08:21 +01:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2019-10-31 19:18:02 +01:00
|
|
|
hints.ai_family = AF_UNSPEC; /* utilisation du serveur sur IPv4/v6 (AF_INET/AF_INET6)*/
|
|
|
|
hints.ai_socktype = SOCK_STREAM; /* socket en mode datagramme */
|
2019-10-29 15:08:21 +01:00
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
s = cree_socket(ip, port, hints); /* création du socket */
|
2019-10-29 15:08:21 +01:00
|
|
|
if(s == 1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:26:03 +01:00
|
|
|
con_serveur(&sfd);
|
2019-10-28 17:31:10 +01:00
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
freeaddrinfo(result); /* libère la structure d'adresse serveur */
|
2019-10-28 17:31:10 +01:00
|
|
|
|
2019-10-31 19:18:02 +01:00
|
|
|
for (j = 3; j < argc; j++) { /* récupère les mots passés en arguments */
|
2019-10-29 15:08:21 +01:00
|
|
|
len = strlen(argv[j]) + 1;
|
2019-10-30 08:24:54 +01:00
|
|
|
argtemp = argv[j];
|
2019-10-31 19:18:02 +01:00
|
|
|
/* +1 pour l'octet null de finde ligne */
|
2019-10-30 08:24:54 +01:00
|
|
|
sendreceive(argv[j], len, j, sfd);
|
2019-10-29 15:08:21 +01:00
|
|
|
}
|
2019-10-30 08:24:54 +01:00
|
|
|
|
2019-10-29 15:08:21 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2019-09-23 14:37:16 +02:00
|
|
|
}
|