Winsock2无法连接到IP

时间:2019-03-19 15:22:41

标签: c++ winsock winsock2

首先,我已经禁用了防火墙,为端口(TCP&UDP)添加了规则,在路由器上转发了端口。

我正在尝试使用winsock创建登录系统,但是每当我输入ipv4时,客户端都不会连接到服务器。但是,如果我使用127.0.0.1,似乎没有发生。 尝试使用具有不同网络的第二台PC时效果不佳。

客户端:

    string ipAddress = myip;            
int port = myport;                      

cout << "========================" << endl;
cout << "client started..." << endl;

WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);
if (wsResult != 0)
{
    cout << "init failed error: " << wsResult << endl;
    return 1;
}

SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
    cout << "cant create socket error: " << WSAGetLastError() << endl;
    WSACleanup();
    return 1;
}

sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);

int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR)
{
    cout << "cant connect to server error: " << WSAGetLastError() << endl;
    closesocket(sock);
    WSACleanup();
    return 1;
}

char buf[4096];
string userInput = "testingfromclient";

int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
if (sendResult != SOCKET_ERROR)
{
    ZeroMemory(buf, 4096);
    int bytesReceived = recv(sock, buf, 4096, 0);
    if (bytesReceived > 0)
    {
        cout << "from server : " << string(buf, 0, bytesReceived) << endl;
    }
}

closesocket(sock);
WSACleanup();

服务器:

cout << "started" << endl;
while (true) {
    cout << "============= connection starting =============" << endl;
    WSADATA wsaData;

    int wsOk = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (wsOk != 0) {
        cerr << "can't init" << endl;
        return 1;
    }

    SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
    if (listening == INVALID_SOCKET)
    {
        cout << "cant create socket" << endl;
        return 1;
    }

    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(port);
    hint.sin_addr.S_un.S_addr = INADDR_ANY; 

    int bindResult = bind(listening, (sockaddr*)&hint, sizeof(hint));

    listen(listening, SOMAXCONN);

    cout << "============= connection started =============" << endl;

    sockaddr_in client;
    int clientSize = sizeof(client);

    SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);

    char host[NI_MAXHOST];      
    char service[NI_MAXSERV];   

    ZeroMemory(host, NI_MAXHOST); 
    ZeroMemory(service, NI_MAXSERV);

    if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
    {
        cout << host << " connected on port " << service << endl;
    }
    else
    {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        cout << host << " connected on port " <<
            ntohs(client.sin_port) << endl;
    }

    closesocket(listening);

    char buf[4096];
    string senddata;

    while (true) {
        ZeroMemory(buf, 4096);

        int bytesReceived = recv(clientSocket, buf, 4096, 0);
        if (bytesReceived > 0) {
            cout << "============= recieved bytes from client =============" << endl;
            printf("Bytes received: %d\n", bytesReceived);
            if (buf == "testingfromclient") {
                senddata += "friend";
            }
            else {
                senddata += "stranger";
            }

            send(clientSocket, senddata.c_str(), bytesReceived, 0);
            std::cout << "senddata: " << senddata << std::endl;
            Sleep(1000);
        }
        else if (bytesReceived == 0) {
            printf("Connection closing...\n");
            break;
        }
        else {
            printf("recv failed with error: %d\n", WSAGetLastError());
            break;
        }

    }

    closesocket(clientSocket);
    WSACleanup();
    std::cout << "============= connection ended =============" << endl;
}
std::cout << "ended" << endl;

return 0;

谢谢

0 个答案:

没有答案