无法解决我的TCP服务器代码中的此错误

问题描述

所以,我试图对这个简单的TCP服务器进行编码,但是我被这个错误所困扰

错误:在此范围内未声明“ inet_ntop”;你是说'inet_ntoa'

我知道我必须使用ntop而不是ntoa。但是我找不到解决方法。我到处搜寻,找不到任何东西。我希望有一个人可以帮助我。我的代码在下面。

#define _WIN32_WINNT 0x501


#include <iostream>
#include <WS2tcpip.h>

#pragma comment (lib,"ws2_32.lib")

using namespace std;

int main(void){

    //initialize winsock
    WSADATA WSData;
    WORD ver = MAKEWORD(2,2);

    int WSOK = WSAStartup(ver,&WSData);
    if (WSOK != 0){
        cerr << "Can't initialize winsock! Quitting" << endl;
        return 0;
    }

    //create a socket
    SOCKET listening = socket(AF_INET,SOCK_STREAM,0);
    if (listening == INVALID_SOCKET)
    {
        cerr << "Can't create a socket! Quitting" << endl;
        return 0;
    }
    
    // bind the socket to an ip adress an port to a socket
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(54000);
    hint.sin_addr.S_un.S_addr = INADDR_ANY; // Could also use inet_pton...

    bind(listening,(sockaddr*)&hint,sizeof(hint));

    //tell winsock the socket is for listening
    listen(listening,SOMAXCONN);


    //wait for connection

    sockaddr_in client;
    int clientSize = sizeof(client);

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

    char host[NI_MAXHOST]; // client's remote name
    char service[NI_MAXSERV]; // Service (i.e port) the client is connected on

    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),NI_MAXHOST);
        cout << host << "connecte on port" <<
            ntohs(client.sin_port) << endl;
    }
    

    //close listening socket
    closesocket(listening);

    //while loop: accept and echo message bac to client
    char buf[4096];

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

        //wait for client send data
        int bytesReceived = recv(clientSocket,buf,4096,0);
        if (bytesReceived == SOCKET_ERROR)
        {
            cerr << "Error in recv(). Quitting" << endl;
            break;
        }
        if (bytesReceived == 0)
        {
            cout << "Client disconnected " << endl;
            break;
        }

        //echo message back to client
        send(clientSocket,bytesReceived + 1,0);

    }
    
    //close socket
    closesocket(clientSocket);

    // cleanup winsock
    WSACleanup();

}

解决方法

inet_ntop()在运行时需要Windows Vista和更高版本,但是您将_WIN32_WINNT设置为0x501(代表Windows XP),因此在{{1 }}在编译时被禁用,以防止程序在运行时无法启动。

您需要将inet_ntop()设置为至少0x600才能启用<w32tcpip.h>


但是,即使解决了该问题,由于调用_WIN32_WINNT时语法有误,因此代码仍将无法编译-第2个参数中的括号不平衡。您可能缺少inet_ntop()还是错误的inet_ntop(),具体取决于您尝试使用以下哪种语法:

(

)


您还会遇到其他逻辑错误,直到运行时才会显示。您没有对inet_ntop(AF_INET,&(client.sin_addr),host,NI_MAXHOST);inet inet_ntop(AF_INET,&client.sin_addr,NI_MAXHOST);bind()进行任何错误处理。而且,listen()上也可能存在缓冲区溢出。