implementation de la fonction cree_socket
This commit is contained in:
parent
74e8b8a04d
commit
bd81cdaedf
164
client_echo.c
164
client_echo.c
@ -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[])
|
|
||||||
{
|
|
||||||
struct addrinfo hints;
|
|
||||||
struct addrinfo *result, *rp;
|
|
||||||
int sfd, s, j;
|
|
||||||
size_t len;
|
|
||||||
ssize_t nread;
|
|
||||||
char buf[BUF_SIZE];
|
|
||||||
|
|
||||||
if (argc < 3) {
|
int cree_socket(char* ip, char* port, struct addrinfo hints){
|
||||||
fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
|
int s = getaddrinfo(ip, port, &hints, &result);
|
||||||
exit(EXIT_FAILURE);
|
if (s != 0) {
|
||||||
}
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
||||||
|
s = 1;
|
||||||
/* Obtain address(es) matching host/port */
|
}
|
||||||
|
return s;
|
||||||
memset(&hints, 0, sizeof(struct addrinfo));
|
}
|
||||||
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
||||||
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
int main(int argc, char *argv[]) {
|
||||||
hints.ai_flags = 0;
|
struct addrinfo hints;
|
||||||
hints.ai_protocol = 0; /* Any protocol */
|
struct addrinfo *rp;
|
||||||
|
int sfd, s, j;
|
||||||
s = getaddrinfo(argv[1], argv[2], &hints, &result);
|
size_t len;
|
||||||
if (s != 0) {
|
ssize_t nread;
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
char buf[BUF_SIZE];
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
if (argc < 3) { // vérification du nombre d'arguments rentrés
|
||||||
|
fprintf(stderr, "mettre: %s ip port message\n", argv[0]);
|
||||||
/* getaddrinfo() returns a list of address structures.
|
exit(EXIT_FAILURE);
|
||||||
Try each address until we successfully connect(2).
|
}
|
||||||
If socket(2) (or connect(2)) fails, we (close the socket
|
|
||||||
and) try the next address. */
|
char* ip = argv[1];
|
||||||
|
char* port = argv[2];
|
||||||
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
char* message = argv[3]; // remplissage des varables a partir des arguments passés au programme
|
||||||
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
||||||
rp->ai_protocol);
|
/* Obtain address(es) matching host/port */
|
||||||
if (sfd == -1)
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
continue;
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
||||||
|
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
||||||
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
hints.ai_flags = 0;
|
||||||
break; /* Success */
|
hints.ai_protocol = 0; /* Any protocol */
|
||||||
|
|
||||||
close(sfd);
|
s = cree_socket(ip, port, hints); // création du socket
|
||||||
}
|
if(s == 1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
if (rp == NULL) { /* No address succeeded */
|
}
|
||||||
fprintf(stderr, "Could not connect\n");
|
|
||||||
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)
|
||||||
freeaddrinfo(result); /* No longer needed */
|
essayons l'adresse suivante */
|
||||||
|
|
||||||
/* Send remaining command-line arguments as separate
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
||||||
datagrams, and read responses from server */
|
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
|
if (sfd == -1){
|
||||||
for (j = 3; j < argc; j++) {
|
continue;
|
||||||
len = strlen(argv[j]) + 1;
|
}
|
||||||
/* +1 for terminating null byte */
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1){
|
||||||
|
break; /* Success */
|
||||||
if (len + 1 > BUF_SIZE) {
|
}
|
||||||
fprintf(stderr,
|
close(sfd);
|
||||||
"Ignoring long message in argument %d\n", j);
|
}
|
||||||
continue;
|
|
||||||
}
|
if (rp == NULL) { /* No address succeeded */
|
||||||
|
fprintf(stderr, "Could not connect\n");
|
||||||
if (write(sfd, argv[j], len) != len) {
|
exit(EXIT_FAILURE);
|
||||||
fprintf(stderr, "partial/failed write\n");
|
}
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
freeaddrinfo(result); /* No longer needed */
|
||||||
|
|
||||||
nread = read(sfd, buf, BUF_SIZE);
|
/* Send remaining command-line arguments as separate
|
||||||
if (nread == -1) {
|
datagrams, and read responses from server */
|
||||||
perror("read");
|
|
||||||
exit(EXIT_FAILURE);
|
for (j = 3; j < argc; j++) {
|
||||||
}
|
len = strlen(argv[j]) + 1;
|
||||||
|
/* +1 for terminating null byte */
|
||||||
printf("Received %ld bytes: %s\n", (long) nread, buf);
|
if (len + 1 > BUF_SIZE) {
|
||||||
}
|
fprintf(stderr, "Ignoring long message in argument %d\n", j);
|
||||||
|
continue;
|
||||||
exit(EXIT_SUCCESS);
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,10 +10,8 @@
|
|||||||
|
|
||||||
struct addrinfo *result; // tableau des adresses réseaux des clients
|
struct addrinfo *result; // tableau des adresses réseaux des clients
|
||||||
|
|
||||||
// to do : traite_connexion
|
int cree_socket(struct addrinfo hints, char* port){
|
||||||
|
int s = getaddrinfo(NULL, port, &hints, &result);
|
||||||
int cree_socket(struct addrinfo hints, char* ip){
|
|
||||||
int s = getaddrinfo(NULL, ip, &hints, &result);
|
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
||||||
s = 1;
|
s = 1;
|
||||||
@ -22,7 +20,7 @@ int cree_socket(struct addrinfo hints, char* ip){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void con_client(int *sfd){
|
void con_client(int *sfd){
|
||||||
struct addrinfo *rp; // structure de l'adresse du client
|
struct addrinfo *rp; // structure de l'adresse du client
|
||||||
/* getaddrinfo () retourne une liste de structures d'adresses.
|
/* getaddrinfo () retourne une liste de structures d'adresses.
|
||||||
Essayez chaque adresse jusqu'à ce que nous ayons réussi à bind(2).
|
Essayez chaque adresse jusqu'à ce que nous ayons réussi à bind(2).
|
||||||
Si socket() (ou bind()) échoue, nous (fermons le socket et)
|
Si socket() (ou bind()) échoue, nous (fermons le socket et)
|
||||||
@ -83,14 +81,14 @@ int main(int argc, char *argv[]){
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* ip = argv[1]; // récupération de l'ip passée en paramètre du programme
|
char* port = argv[1]; // récupération du port passée en paramètre du programme
|
||||||
|
|
||||||
// remplissage de la structure hints contenants la configuration réseau du serveur
|
// remplissage de la structure hints contenants la configuration réseau du serveur
|
||||||
memset(&hints, 0, sizeof(struct addrinfo));
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
hints.ai_family = AF_INET6; // utilisation du serveur sur IPv6 (AF_UNSPEC pour IPv4/6)
|
hints.ai_family = AF_UNSPEC; // utilisation du serveur sur IPv4/v6 (AF_INET/AF_INET6)
|
||||||
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
|
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
|
||||||
|
|
||||||
s = cree_socket(hints, ip); // création du socket
|
s = cree_socket(hints, port); // création du socket
|
||||||
if(s == 1) {
|
if(s == 1) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user