|
|
@ -8,85 +8,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
#define BUF_SIZE 500
|
|
|
|
#define BUF_SIZE 500
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
struct addrinfo *result; // tableau des adresses réseaux des serveurs
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
|
|
|
{
|
|
|
|
int cree_socket(char* ip, char* port, struct addrinfo hints){
|
|
|
|
struct addrinfo hints;
|
|
|
|
int s = getaddrinfo(ip, port, &hints, &result);
|
|
|
|
struct addrinfo *result, *rp;
|
|
|
|
if (s != 0) {
|
|
|
|
int sfd, s, j;
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
size_t len;
|
|
|
|
s = 1;
|
|
|
|
ssize_t nread;
|
|
|
|
}
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
|
|
|
|
struct addrinfo hints;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
struct addrinfo *rp;
|
|
|
|
|
|
|
|
int sfd, s, j;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (argc < 3) { // vérification du nombre d'arguments rentrés
|
|
|
|
|
|
|
|
fprintf(stderr, "mettre: %s ip port message\n", argv[0]);
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* ip = argv[1];
|
|
|
|
|
|
|
|
char* port = argv[2];
|
|
|
|
|
|
|
|
char* message = argv[3]; // remplissage des varables a partir des arguments passés au programme
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Obtain address(es) matching host/port */
|
|
|
|
|
|
|
|
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 = 0;
|
|
|
|
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s = cree_socket(ip, port, hints); // création du socket
|
|
|
|
|
|
|
|
if(s == 1) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1){
|
|
|
|
/* Obtain address(es) matching host/port */
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
|
|
|
|
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 = 0;
|
|
|
|
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s = getaddrinfo(argv[1], argv[2], &hints, &result);
|
|
|
|
|
|
|
|
if (s != 0) {
|
|
|
|
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
Try each address until we successfully connect(2).
|
|
|
|
fprintf(stderr, "Could not connect\n");
|
|
|
|
If socket(2) (or connect(2)) fails, we (close the socket
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
and) try the next address. */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
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)
|
|
|
|
/* Send remaining command-line arguments as separate
|
|
|
|
break; /* Success */
|
|
|
|
datagrams, and read responses from server */
|
|
|
|
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
for (j = 3; j < argc; j++) {
|
|
|
|
|
|
|
|
len = strlen(argv[j]) + 1;
|
|
|
|
|
|
|
|
/* +1 for terminating null byte */
|
|
|
|
|
|
|
|
if (len + 1 > BUF_SIZE) {
|
|
|
|
|
|
|
|
fprintf(stderr, "Ignoring long message in argument %d\n", j);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(sfd, argv[j], len) != len) {
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
fprintf(stderr, "partial/failed write\n");
|
|
|
|
fprintf(stderr, "Could not connect\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = read(sfd, buf, BUF_SIZE);
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
if (nread == -1) {
|
|
|
|
|
|
|
|
perror("read");
|
|
|
|
/* Send remaining command-line arguments as separate
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
datagrams, and read responses from server */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (j = 3; j < argc; j++) {
|
|
|
|
|
|
|
|
len = strlen(argv[j]) + 1;
|
|
|
|
|
|
|
|
/* +1 for terminating null byte */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (len + 1 > BUF_SIZE) {
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
|
|
|
"Ignoring long message in argument %d\n", j);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (write(sfd, argv[j], len) != len) {
|
|
|
|
|
|
|
|
fprintf(stderr, "partial/failed write\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nread = read(sfd, buf, BUF_SIZE);
|
|
|
|
|
|
|
|
if (nread == -1) {
|
|
|
|
|
|
|
|
perror("read");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("Received %ld bytes: %s\n", (long) nread, buf);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Received %ld bytes: %s\n", (long) nread, buf);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|