【问题标题】:Cannot solve this error in my TCP server code无法在我的 TCP 服务器代码中解决此错误
【发布时间】:2020-10-01 03:15:55
【问题描述】:

所以,我试图编写这个简单的 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), host, 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, buf, bytesReceived + 1, 0);

    }
    
    //close socket
    closesocket(clientSocket);

    // cleanup winsock
    WSACleanup();

}

【问题讨论】:

    标签: c++ networking server tcp tcpserver


    【解决方案1】:

    inet_ntop() 在运行时需要 Windows Vista 及更高版本,但您将 _WIN32_WINNT 设置为代表 Windows XP 的 0x501,因此在编译时禁用 &lt;w32tcpip.h&gt; 中的 inet_ntop() 声明以防止程序在运行时无法启动。

    您需要将_WIN32_WINNT 设置为至少0x600 以启用inet_ntop()


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

    inet_ntop(AF_INET, &amp;(client.sin_addr), host, NI_MAXHOST);

    inet inet_ntop(AF_INET, &amp;client.sin_addr, host, NI_MAXHOST);


    您还有其他直到运行时才会出现的逻辑错误。您没有对bind()listen()send() 进行任何错误处理。而且send() 上也有潜在的缓冲区溢出。

    【讨论】:

    • 我换了windows版本,谢谢。而且我知道缺乏错误处理。但不幸的是,我不断收到“错误:'inet_ntop' 未在此范围内声明;您的意思是 'inet_ntoa'”。老实说,我不知道还能做什么,在整个互联网上搜索了几个小时,似乎没有什么可以解决这个问题,我也尝试了你的两种语法。
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多