因此,我将 Windows 8 防火墙配置为允许 IPV6 出站和入站连接作为预防措施,但是每当我尝试使用环回地址(连接到同时运行的 ipv6 服务器应用程序)连接到远程主机或我自己的机器时,连接功能都会失败无论我选择什么端口,都会出现错误 10049。这是网络配置问题吗?IP4 工作得很好。
#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <string>
#include <stdlib.h>
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <WS2tcpip.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <IPTypes.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib,"iphlpapi.lib")
using namespace std;
static int MAXPENDING = 5;
void set_interface(SOCKET s, DWORD Index);
int main(int argc,char* argv[])
{
WSADATA wsaData;
int Result;
int optv = 1;
char *optval = (char*)&optv;
int optlen = sizeof(optval);
DWORD Index = 22;
bool connected = false;
string exit = "quit";
string name;
sockaddr_in6 c_addr;
c_addr.sin6_family = AF_INET6;
char buffer[1024];
Result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (Result != NO_ERROR) {
printf("WSAStartup function failed with error: %d\n", GetLastError());
return 0;
}
SETUP:
SOCKET connector;
connector = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (connector == INVALID_SOCKET) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
closesocket(connector);
return 1;
}
cout << "Enter host name(URL): ";
cin >> name;
cout << endl;
char *hostname = const_cast<char*>(name.c_str());
if(inet_pton(AF_INET6,hostname,&c_addr.sin6_addr)<0){
printf("InetPton() function failed with error: %d/n",GetLastError());
closesocket(connector);
return 1;
}
string p;
char *hostport;
int p0rt;
cout << "Enter the port number you wish to connect on: " ;
cin >> p;
cout << endl;
hostport = const_cast<char*>(p.c_str());
p0rt = atoi(hostport);
if(!( 0 < p0rt && p0rt < 65,536)){
cout << "Error: port number must be greater than 0 and less than 65,536. " << endl;
cout << endl;
cout << "Re-enter the port number you wish to connect on: " ;
cin >> p;
cout << endl;
hostport = const_cast<char*>(p.c_str());
p0rt = atoi(hostport);
}else{
c_addr.sin6_port = htons(p0rt);
}
if(connect(connector,(sockaddr*)&c_addr,sizeof(c_addr))<0){
printf("Connect failed with error: %d\n",GetLastError());
string cmd;
cout << endl;
cout <<"Try again? Type 'y' to reattempt connection or type any other key to exit: ";
cin >> cmd;
cout << endl;
if(cmd=="y"){
goto SETUP;
}else {
goto QUIT;
}
}else{
connected = true;
cout << "Connected to host " << hostname << " on port " << c_addr.sin6_port << endl;
cout << "Type 'quit' to exit the program " << endl;
}
while(connected==true){
int nbr = 0;
cout << endl;
cout << ">";
scanf("%s",buffer);
cout << endl;
string str(buffer);
if(str==exit){
connected = false;
goto QUIT;
}
long nbs = send(connector,buffer,sizeof(buffer),0);
if(nbs < 0){
printf("send() failed", GetLastError());
goto QUIT;
}
while(nbr < nbs){
nbr = recv(connector,buffer,sizeof(buffer),0);
if(nbr < 0){
printf("recv() failed", GetLastError());
goto QUIT;
}else if(nbr==0){
printf("recv() failed: connection closed prematurely", GetLastError());
goto QUIT;
}else if(nbr > 0){
cout << endl;
cout << ">> ";
printf(buffer);
cout << endl;
}
}
}
QUIT:
if (closesocket(connector) == SOCKET_ERROR) {
printf("closesocket function failed with error: %ld\n", GetLastError());
WSACleanup();
return 1;
}
WSACleanup();
return 0;
};
void set_interface(SOCKET s, DWORD Index){
MIB_IF_ROW2 T2;
NET_LUID ID;
DWORD ptbuflen = (DWORD)sizeof(T2);
ConvertInterfaceIndexToLuid(Index,&ID);
T2.InterfaceLuid = ID;
GetIfEntry2(&T2);
if(WSAIoctl(s,SIO_ASSOCIATE_HANDLE,&T2,ptbuflen,NULL,0,0,NULL,NULL)>0){
printf("WSAIoctl() function failed with error: %d\n", GetLastError());
closesocket(s);
}
if(WSAIoctl(s,SIO_TRANSLATE_HANDLE,&T2,ptbuflen,NULL,0,0,NULL,NULL)>0){
printf("WSAIoctl() function failed with error: %d\n", GetLastError());
closesocket(s);
}
};