This question already has answers here:
Closed last year.
How to give to a client specific ip address in C
(2个答案)
我的电脑有两个IP地址,可以装两个以太网卡。192.168.0.1是服务器,但如何在192.168.0.2中使用Cwinsock2设置为客户端。默认情况下,客户端总是192.168.0.1

最佳答案

您可以在bind之前调用connect,为客户端提供特定的IP地址或端口。

sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("192.168.0.2"); // bind to specific IP address
service.sin_port = 0; // not to specify port number

iResult = bind(ListenSocket, (SOCKADDR *) &service, sizeof (service));

// then call connect()...

关于c - 如何使用WinSock2在C中为客户端设置IP地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50712860/

10-15 02:54