Client et Serveur en UDP Ipv6
bonjour, je suis entrain de développé un client et un serveur en udp ipv6,
cependant je rencontre quelques difficultés au niveau de la compréhension de
getaddrinfo. Je dois envoyé une chaine de caractère entre un client et un serveur.
Je me pose plusieurs questions:
côté client:
- Je voudrais savoir si getaddrinfo est bien utilisé (côté client) sachant les paramètres de ligne de commandes qu'il faut utiliser?
- Faut-t-il initialisé une struct sockaddr_in6 côté client?
- Je voudrais savoir si en utilisant getaddrinfo coté client, on n'a pas besoin d'initialisé un port?
voici l'erreur de getaddrinfo (coté client) : Address family for hostname not supported
côté serveur :
- Je voudrais savoir si getaddrinfo est bien utilisé (côté serveur) sachant les paramètres de ligne de commandes qu'il faut utiliser?
- Je voudrais savoir, si on peut utilisé les informations de getaddrinfo pour l'utilisation du bind?
- Faut-t-il utilisé et initialisé une struct sockaddr_in6 coté serveur?
voici l'erreur de getaddrinfo (côté serveur) : Servname not supported for ai_socktype
Voici le client udp en ipv6 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
// client UDP en Ipv6
int main(int argc, char **argv)
{
int sockfd = 0;
int get = 0;
socklen_t addrlen;
struct sockaddr_in6 dest;
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *rp;
addrlen = sizeof(struct sockaddr_in6);
// check the number of args on command line
if(argc != 4)
{
printf("USAGE: %s @dest port string\n", argv[0]);
exit(-1);
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6; /* IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_UDP; /* UDP protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
get = getaddrinfo( argv[1], NULL, &hints,&res);
if(get!=0)
{
printf("getaddrinfo: %s\n", gai_strerror(get));
exit(EXIT_FAILURE);
}
// for each of the address records returned, attempt to set up a socket.
for ( rp = res ; rp!= NULL; rp = rp->ai_next)
{
if( ( sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol) ) == -1)
{
perror("socket");
close(sockfd);
exit(EXIT_FAILURE);
}
}
// get addr from command line and convert it
if(inet_pton(AF_INET6,argv[1], &(dest.sin6_addr)) != 1)
{
perror("inet_pton");
close(sockfd);
exit(EXIT_FAILURE);
}
// send string
if(sendto(sockfd, argv[3], strlen(argv[3])+1, 0, (struct sockaddr *)&dest, addrlen) == -1)
{
perror("sockfd");
close(sockfd);
exit(EXIT_FAILURE);
}
printf("Send ok!");
// close the socket
close(sockfd);
freeaddrinfo(res);
return 0;
} |
Voici le serveur udp en ipv6 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
//serveur UDP en Ipv6
int main(int argc, char **argv)
{
int sockfd = 0;
char buf[1024];
socklen_t addrlen;
// struct sockaddr_in6 my_addr;
struct sockaddr_in6 client;
int get;
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *res0;
// check the number of args on command line
if(argc != 2)
{
printf("Usage: %s service\n", argv[0]);
exit(-1);
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6; /* IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_UDP; /* UDP protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
get = getaddrinfo(NULL, argv[1], &hints,&res0);
if(get!=0)
{
printf("getaddrinfo: %s\n", gai_strerror(get));
exit(EXIT_FAILURE);
}
// for each of the address records returned, attempt to set up a socket.
for (res = res0; res != NULL; res = res->ai_next)
{
if( ( sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
{
perror("socket");
close(sockfd);
exit(EXIT_FAILURE);
}
// bind addr structure with socket
if(bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
{
perror("bind");
close(sockfd);
exit(EXIT_FAILURE);
}
}
memset(buf,'\0',1024);
// reception de la chaine de caracteres
if(recvfrom(sockfd, buf, 1024, 0,(struct sockaddr*)&client , &addrlen) == -1)
{
perror("recvfrom");
close(sockfd);
exit(EXIT_FAILURE);
}
// print the received char
printf("chaine recu :%s\n",buf);
// close the socket
close(sockfd);
freeaddrinfo(res);
return 0;
} |
Merci de votre compréhension.