csocketsposixipv6

Is it possible to bind a socket to 2 adresses in c?


I've to create a socket which listens to two IP adresses. Is it possibile?

struct sockaddr_in6 bind_addr;
memset(&bind_addr, 0, sizeof(bind_addr));

bind_addr.sin6_family = AF_INET6;
bind_addr.sin6_port   = htons(9001);
bind_addr.sin6_addr   = in6addr_any;

if (bind(fd,(struct sockaddr *) &bind_addr,sizeof(bind_addr)) < 0) {
    perror("bind() failed");
    return -1;
}

I need something like "in6addr_any", which listens from all the IPs but only for two of them.


Solution

  • No, you have to use two different sockets for that; bind each socket to one of the addresses.