我一直在努力轉換一個小的Windows C++ IPv4程序是IPv6兼容,並一直在我的頭撞我的時間。有些東西會導致任何IPv6功能失敗。C/C++套接字:IPv6 TCP連接失敗,如果我做任何事情
我寫了一個簡單的服務器/客戶端程序,複製錯誤。如果我在調用客戶端/服務器函數之前做了任何事情,那麼套接字函數失敗。
服務器將在綁定函數上獲得WSA 10049錯誤,並且客戶端將在連接函數上獲得WSA 10049錯誤。
但是,如果代碼被註釋掉,它會成功。
究竟是怎麼回事?我覺得我必須錯過一些非常簡單的事情。
我使用g ++來編譯最新版本的MINGW。
編輯:它的工作原理永遠如果我改變了代碼回使用IPv4/AF_INET
server.cpp
#include <w32api.h>
#define WINVER WindowsVista
#define _WIN32_WINDOWS WindowsVista
#define _WIN32_WINNT WindowsVista
#include <winsock2.h>
#include <ws2tcpip.h>
int setupWinSock(){
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
return 1;
}
return 0;
}
void simpleServer(int port){
printf("SERVER START\n");
int s, c;
int reuseaddr = 1;
struct sockaddr_in6 addr;
int pid;
s = socket(AF_INET6, SOCK_STREAM, 0);
if (s == SOCKET_ERROR){
printf("socket ERROR IPV6: %d\n", WSAGetLastError());
return;
}
int optval = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof optval);
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
addr.sin6_addr = in6addr_any;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0){
printf("bind ERROR IPV6: %d\n", WSAGetLastError());
return;
}
if (listen(s, 5) < 0){
printf("listen ERROR IPV6: %d\n", WSAGetLastError());
return;
}
c = accept(s, NULL, NULL);
if (c == SOCKET_ERROR) printf("ACCEPT ERROR IPV6: %d\n", WSAGetLastError());
else printf("It works!\n");
closesocket(s);
closesocket(c);
}
int main(){
if (setupWinSock()){
abort();
}
// ANYTHING HERE makes socket functions fail
// Could be this
//double tmp = 100000;
// tmp = tmp * tmp;
// This to....
//std::this_thread::sleep_for(std::chrono::milliseconds(2000));
simpleServer(8080);
WSACleanup();
}
client.cpp
#include <w32api.h>
#define WINVER WindowsVista
#define _WIN32_WINDOWS WindowsVista
#define _WIN32_WINNT WindowsVista
#include <winsock2.h>
#include <ws2tcpip.h>
int setupWinSock(){
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
return 1;
}
return 0;
}
void simpleClient(int port){
printf("CLIENT START\n");
int s, x;
struct sockaddr_in6 addr;
s = socket(AF_INET6, SOCK_STREAM, 0);
if (s == SOCKET_ERROR){
printf("socket ERROR IPV6: %d\n", WSAGetLastError());
return;
}
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
inet_pton(AF_INET6, "::1", &addr.sin6_addr);
x = connect(s, (struct sockaddr *)&addr, sizeof(addr));
if (x == SOCKET_ERROR) printf("CONNECT ERROR IPV6: %d\n", WSAGetLastError());
else printf("It works!\n");
closesocket(s);
}
int main(){
if (setupWinSock()){
abort();
}
// ANYTHING HERE makes socket functions fail
// Could be this
//double tmp = 100000;
// tmp = tmp * tmp;
// This to....
//std::this_thread::sleep_for(std::chrono::milliseconds(2000));
simpleClient(8080);
WSACleanup();
}
請不要垃圾郵件標籤,即使這與慣用的C++完全相反,它仍然是C++,而不是C。 – George
沒有語言叫'C/C++' – user463035818
他已經使用了C和C++樣式聲明結構變量 – Asesh