Orum
July 16th, 2005, 11:04 AM
I'm trying to bind an IPv6 socket to any IPv6 address, but I'm running into problems. For instance, here's how I do it in IPv4
SOCKADDR_IN SockAddr4;
SockAddr4.sin_family = AF_INET;
SockAddr4.sin_addr.s_addr = INADDR_ANY;
SockAddr4.sin_port = htons(port);
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
if(bind(Socket, (SOCKADDR*)&SockAddr4, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv4 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
So, when I bind to an IPv6 socket, I did it very similarly....
const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
SOCKADDR_IN6 SockAddr6;
SockAddr6.sin6_family = AF_INET6;
SockAddr6.sin6_addr = in6addr_any;
SockAddr6.sin6_port = htons(port);
Socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
if(bind(Socket, (SOCKADDR*)&SockAddr6, sizeof(SOCKADDR_IN6)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv6 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
Now for some reason, the IPv4 binding works just fine, but the IPv6 binding is always giving me WSAEADDRNOTAVAIL when I check WSAGetLastError(). I have IPv6 installed, and the sample IPv6 server code @ MSDN works just fine (and yes, I remembered to shut it down before attempting to test my program). However, I noticed that in their server code they get a list of addresses that the local computer has and then binds to them, but I just want to bind to the IPv6 equivalent of IPv4's IPADDR_ANY (IPv6 address ::).
Is there anything I'm doing wrong in my IPv6 code that's causing me to get WSAEADDRNOTAVAIL every time I try to bind to IPv6 address ::?
SOCKADDR_IN SockAddr4;
SockAddr4.sin_family = AF_INET;
SockAddr4.sin_addr.s_addr = INADDR_ANY;
SockAddr4.sin_port = htons(port);
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
if(bind(Socket, (SOCKADDR*)&SockAddr4, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv4 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
So, when I bind to an IPv6 socket, I did it very similarly....
const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
SOCKADDR_IN6 SockAddr6;
SockAddr6.sin6_family = AF_INET6;
SockAddr6.sin6_addr = in6addr_any;
SockAddr6.sin6_port = htons(port);
Socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
if(bind(Socket, (SOCKADDR*)&SockAddr6, sizeof(SOCKADDR_IN6)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv6 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}
Now for some reason, the IPv4 binding works just fine, but the IPv6 binding is always giving me WSAEADDRNOTAVAIL when I check WSAGetLastError(). I have IPv6 installed, and the sample IPv6 server code @ MSDN works just fine (and yes, I remembered to shut it down before attempting to test my program). However, I noticed that in their server code they get a list of addresses that the local computer has and then binds to them, but I just want to bind to the IPv6 equivalent of IPv4's IPADDR_ANY (IPv6 address ::).
Is there anything I'm doing wrong in my IPv6 code that's causing me to get WSAEADDRNOTAVAIL every time I try to bind to IPv6 address ::?