c++ - Winsock 服务器和客户端示例 : "getaddrinfo" was not declared in this scope

标签 c++ compiler-errors server codeblocks winsock

我正在尝试通过简单的服务器和客户端使用 C++ 进行网络连接,从一些 Winsock 示例开始。

每当我尝试使用 Code::Blocks 编译服务器时,都会收到以下错误:

||=== Build: Debug in Server Test (compiler: GNU GCC Compiler) ===|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|12|warning: ignoring #pragma comment  [-Wunknown-pragmas]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp||In function 'int main()':|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|47|error: 'getaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|57|warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int' [-Wformat]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|58|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|67|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|73|error: 'freeaddrinfo' was not declared in this scope|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

我已经尝试过 this question 的解决方案但是,这会导致 36 个错误。

我的客户端代码也遇到同样的问题。

服务器代码:

#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(void)
{
    WSADATA wsaData;
    int iResult;

    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;

    struct addrinfo *result = NULL;
    struct addrinfo hints;

    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a SOCKET for connecting to server
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Setup the TCP listening socket
    iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed with error: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // No longer need server socket
    closesocket(ListenSocket);

    // Receive until the peer shuts down the connection
    do {

        iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0) {
            printf("Bytes received: %d\n", iResult);

        // Echo the buffer back to the sender
            iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
            if (iSendResult == SOCKET_ERROR) {
                printf("send failed with error: %d\n", WSAGetLastError());
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            }
            printf("Bytes sent: %d\n", iSendResult);
        }
        else if (iResult == 0)
            printf("Connection closing...\n");
        else  {
            printf("recv failed with error: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

    } while (iResult > 0);

    // shutdown the connection since we're done
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

最佳答案

如果有人仍在解决这个问题。您所要做的就是以下操作:

  • 在代码开头定义 _WIN32_WINNT,如下所示:#define _WIN32_WINNT 0x501(由 ws2tcpip.h 使用)
  • 按如下方式编译:g++ prog.c -o prog -lws2_32

这些步骤是在代码、服务器和客户端上进行的。

希望这能有所帮助。

关于c++ - Winsock 服务器和客户端示例 : "getaddrinfo" was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420044/

相关文章:

从 backtrace_symbols 接收到的 c++ 符号不显示 g++ 中的函数

java - 奇怪的 Java 错误放置

android - 为什么Eclipse无法编译Google示例?

linux - 如何在 linux (debian) 上获取文件访问信息

python - 蓝牙服务器接收到错误的文本数据 RFCOMM

c++ - 将 int[][] 分配给 int**

c++ - QKeyEvent keyPressEvent 没有检测到方向键?

mysql - 我似乎无法让 rails server 运行

c++ - 解决我的代码中未通过测试用例的错误

海湾合作委员会错误: [enum] field declared as a function