My application needs to bind a socket on the same port with the same IPv6 address. I am using the code below to achieve the same. However second bind throws an error 'Address already in use'. BTW, i get this error only if I include the listen call for the first socket. Without the listen call, the second socket binds just fine.
What I am doing wrong? Please help me understand.
Thanks
int fd1 = ::socket(AF_INET6, SOCK_STREAM, 0);
if (fd1 < 0)
{
perror("fd1 socket()");
return -1;
}
// Set SO_REUSEADDR for both sockets
int reuse = 1;
if (fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK) <0)
{
perror("fd1 fcntl64 failed");
return -1;
}
if (::setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
perror("fd1 SO_REUSEADDR failed");
return -1;
}
if (::setsockopt(fd1, SOL_IPV6, IPV6_V6ONLY, &reuse, sizeof(reuse)) < 0)
{
perror("fd1 SO_REUSEADDR failed");
return -1;
}
sockaddr_storage storage;
socklen_t addrlen = sizeof(storage);
memset(&storage, 0, addrlen);
sockaddr_in6& addr = reinterpret_cast<sockaddr_in6&>(storage);
addr.sin6_family = AF_INET6;
addr.sin6_port = 143;
addr.sin6_addr = in6addr_any;
sockaddr* pAddr = reinterpret_cast<sockaddr*>(&storage);
int val = 2;
socklen_t len = sizeof(val);
if (::getsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &val, &len) < 0)
{
perror("fd1 getsock failed");
return -1;
}
printf("Getsockopt returned %d at %d\n",val, __LINE__);
if (::bind(fd1, pAddr, sizeof(sockaddr_in6)) < 0)
{
perror("bind fd1 failed");
return -1;
}
if (::getsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &val, &len) < 0)
{
perror("fd1 getsock failed");
return -1;
}
printf("Getsockopt returned %d at %d\n",val, __LINE__);
if (listen(fd1, 128) < 0)
{
perror("fd1, listen");
return -1;
}
if (::getsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &val, &len) < 0)
{
perror("fd1 getsock failed");
return -1;
}
printf("Getsockopt returned %d at %d\n",val, __LINE__);
// Get the local address for fd1
addrlen = sizeof(storage);
if (::getsockname(fd1, pAddr, &addrlen))
{
perror("getsockname for fd1 failed");
return -1;
}
char straddr[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, &addr.sin6_addr, straddr, sizeof(straddr)))
{
perror("inet_ntop for fd1 failed");
return -1;
}
printf("fd1=%d addr=%s:%d\n", fd1, straddr, addr.sin6_port);
addrlen = sizeof(storage);
addr.sin6_family = AF_INET6;
addr.sin6_port = 143;
int fd2 = ::socket(AF_INET6, SOCK_STREAM, 0);
if (fd2 < 0)
{
perror("fd2 socket()");
return -1;
}
if (::setsockopt(fd2, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
perror("fd2 SO_REUSEADDR failed");
return -1;
}
if (::setsockopt(fd2, SOL_IPV6, IPV6_V6ONLY, &reuse, sizeof(reuse)) < 0)
{
perror("fd1 SO_REUSEADDR failed");
return -1;
}
if (::bind(fd2, pAddr, sizeof(sockaddr_in6)) < 0)
{
perror("bind fd2 failed");
return -1;
}
// Get the local address for fd2
if (::getsockname(fd2, pAddr, &addrlen))
{
perror("getsockname for fd2 failed");
return -1;
}
if (!inet_ntop(AF_INET6, &addr.sin6_addr, straddr, sizeof(straddr)))
{
perror("inet_ntop for fd2 failed");
return -1;
}
printf("fd2=%d addr=%s:%d\n", fd2, straddr, addr.sin6_port);
return 0;