使用Windows XP我将套接字绑定到特定IP,这应该强制连接使用该接口而不是默认路由表。
即使绑定没有返回错误,连接仍然使用默认路由(在本例中为10.5.0.2)而不是绑定接口(在本例中为10.0.2.2)。
Win8上的一切正常,但XP似乎忽略了绑定。
我怀疑路由表有问题。也许是指标,但我看不出是什么。这是我的测试代码,我正在运行以下args:
tcp.bind 10.0.2.15
此代码连接到50.57.66.64:25并打印服务器响应,该服务器响应只是客户端的IP地址,我用它来测试实际使用的IF。
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
int handle = (int)socket(AF_INET, SOCK_STREAM, 0);
// if they give us a bind port then bind
if (argc > 1)
{
struct sockaddr_in sa_loc;
memset(&sa_loc, 0, sizeof(struct sockaddr_in));
sa_loc.sin_family = PF_INET;
sa_loc.sin_port = htons(0);
sa_loc.sin_addr.s_addr = inet_addr((const char*)argv[1]);
int bindResult = bind(handle, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr));
printf("Bind Result: %d\n", bindResult);
}
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("50.57.66.64");
clientService.sin_port = htons(25);
const int result = ::connect(handle, (SOCKADDR *)& clientService, sizeof (clientService));
char data[1024];
memset(data, 0x00, 1024);
recv(handle, data, 1024, 0);
printf(data);
closesocket(handle);
WSACleanup();
return 0;
}
这是我的路由表:
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 10.0.2.2 10.0.2.15 300
0.0.0.0 0.0.0.0 10.5.0.2 10.5.0.1 6
10.0.2.0 255.255.255.0 10.0.2.15 10.0.2.15 20
10.0.2.15 255.255.255.255 127.0.0.1 127.0.0.1 20
10.5.0.0 255.255.255.0 10.5.0.1 10.5.0.1 30
10.5.0.1 255.255.255.255 127.0.0.1 127.0.0.1 30
10.255.255.255 255.255.255.255 10.0.2.15 10.0.2.15 20
10.255.255.255 255.255.255.255 10.5.0.1 10.5.0.1 30
46.165.236.206 255.255.255.255 10.0.2.2 10.0.2.15 5
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
224.0.0.0 240.0.0.0 10.0.2.15 10.0.2.15 20
224.0.0.0 240.0.0.0 10.5.0.1 10.5.0.1 30
255.255.255.255 255.255.255.255 10.0.2.15 10.0.2.15 1
255.255.255.255 255.255.255.255 10.5.0.1 10.5.0.1 1
Default Gateway: 10.5.0.2