I have this simple client-server application pair. The code is pretty simple, I only use new, recommended methods like getaddinfo etc., and everything works fine for ipv4. Even for ipv6 loopback (:: 1) it works. Problems start when it comes to some other ipv6 addresses ... I have two machines on the network, everything works fine when I pass their ipv4 addresses, but when I give the address of my ipv6 client, I get an error message when connection: invalid argument, hey, don't I know that? I do! When I try to ping6 this ipv6 address, I get the same error:
connect: Invalid argument
But there is a way to overcome this block - you need to choose an interface with the -I switch, and since then everything has been working smoothly. But how can I do the same in my client application? What should I do? My client code is as follows:
struct addrinfo hints; struct addrinfo *server; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; int status; if((status = getaddrinfo(argv[1], argv[2], &hints, &server) != 0)) { perror("getaddrinfo error"); return 1; } int sock_fd; struct addrinfo *ptr; for(ptr=server;ptr!=NULL;ptr=ptr->ai_next) { if( (sock_fd = socket(ptr->ai_family,ptr->ai_socktype,ptr->ai_protocol)) == -1) { perror("socket error"); continue; } if( connect(sock_fd, ptr->ai_addr,ptr->ai_addrlen) == -1 ) { perror("connect error"); continue; } break; }
source share