我用C ++创建了一个客户端和服务器。它们可以在同一台计算机上运行,但我也需要它们在不同的计算机上运行。可以使用当前代码来完成此操作,还是需要以某种方式编辑它?
服务器代码:
#define HOST_PORT "27015"
WSADATA wsaData;
int status;
struct addrinfo * data;
struct addrinfo hints;
WSAStartup(MAKEWORD(WINSOCK_MAJOR_VERSION, WINSOCK_MINOR_VERSION), &wsaData);
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_IP;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(NULL, HOST_PORT, &hints, &data))
{
std::cout << "Unable to translate host name to address\n";
WSACleanup();
}
客户代码:
#define DEFAULT_PORT "27015"
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *result = NULL;
int iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult != 0)
{
std::cout << ("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
}