You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.1 KiB
C
105 lines
3.1 KiB
C
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#define BUF_SIZE 500
|
|
|
|
struct addrinfo *result; // tableau des adresses réseaux des serveurs
|
|
|
|
int cree_socket(char* ip, char* port, struct addrinfo hints){
|
|
int s = getaddrinfo(ip, port, &hints, &result);
|
|
if (s != 0) {
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
s = 1;
|
|
}
|
|
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 à connect(2).
|
|
Si socket() (ou connect()) é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);
|
|
}
|
|
}
|
|
|
|
void sendreceive(char* argtemp, size_t len, int j, int sfd){
|
|
char buf[BUF_SIZE];
|
|
ssize_t nread;
|
|
/* Send remaining command-line arguments as separate
|
|
datagrams, and read responses from server */
|
|
if (len + 1 > BUF_SIZE) {
|
|
fprintf(stderr, "Ignoring long message in argument %d\n", j);
|
|
}
|
|
if (write(sfd, argtemp, 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);
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
struct addrinfo hints;
|
|
int sfd, s, j;
|
|
size_t len;
|
|
char* argtemp;
|
|
|
|
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; // utilisation du serveur sur IPv4/v6 (AF_INET/AF_INET6)
|
|
hints.ai_socktype = SOCK_STREAM; // socket en mode datagramme
|
|
hints.ai_protocol = IPPROTO_TCP; // protocol de connexion
|
|
|
|
s = cree_socket(ip, port, hints); // création du socket
|
|
if(s == 1) {
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
con_serveur(&sfd);
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
for (j = 3; j < argc; j++) { // récupère les mots passés en arguments
|
|
len = strlen(argv[j]) + 1;
|
|
argtemp = argv[j];
|
|
/* +1 for terminating null byte */
|
|
sendreceive(argv[j], len, j, sfd);
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
}
|