0

I'm a problem with my code...

I followed a Youtube tuto (link : https://www.youtube.com/watch?v=WDn-htpBlnU)

In the first time, I copied this code before to edit, but I have an error when copying.

The file is :

#include <iostream>
#include <ws2tcpip.h>


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

using namespace std;

int main () {

    // Initialisation de Winsock
    
    WSADATA wsData;
    WORD ver = MAKEWORD(2,2);

    int wsOk = WSAStartup(ver, &wsData);
    if (wsOk != 0) {
        std::cerr << "Can't Initialize winsock! Quitting" << std::endl;
        return 1;
        
    }


    // Création de la socket

    SOCKET listening = socket (AF_INET, SOCK_STREAM, 0);
    if (listening == INVALID_SOCKET) {
        cerr << "On ne peut pas créer de socker... Fermeture !" << endl;
        return 1;
    }

    // Adresse IP

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

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

    // Socket à l'écoute

    listen(listening, SOMAXCONN);

    // Attente de connexion

    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 << " Connecté au port " << service << endl;
    }
    else {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        cout << host << " Connecté au port " << ntohs(client.sin_port) << endl;
    }

    // Fermeture de l'ecoute de la socket

    closesocket(listening);

    // Boucle While : Accepte et affiche le message du client

    char buf[4096];

    while(true) {
        ZeroMemory(buf, 4096);
        
        // Attente que le client envoi les données
        int bytesReceivied = recv(clientSocket, buf, 4096, 0);
        if (bytesReceivied == SOCKET_ERROR) {
            cerr << " Erreur dans le recv()... Fermeture !" << endl;
            break;
        }

        if (bytesReceivied == 0) {
            cout << " Client deconnecté " << endl;
            break;
        }

        send(clientSocket, buf, bytesReceivied + 1, 0);
    }

    closesocket(clientSocket);

    WSACleanup();

    return 0;
    
}

The error is :

.\TCPServer.cpp: In function 'int main()':

.\TCPServer.cpp:59:97: error: 'getnameinfo' was not declared in this scope
     if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) {
                                                                                                        

.\TCPServer.cpp:63:62: error: 'inet_ntop' was not declared in this scope
         inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);        
user4581301
  • 33,082
  • 7
  • 33
  • 54
Rabah
  • 1
  • 1
    Try adding `#include ` before `#include ` – 001 Jul 25 '22 at 14:50
  • 1
    Might have to specify the minimum Windows version you're compiling for. Give [Winsock Server And Client Example: "getaddrinfo" was not declared in this scope](https://stackoverflow.com/q/36420044/4581301) a try. – user4581301 Jul 25 '22 at 14:50
  • A warning about `#pragma comment (lib, "ws2_32.lib")`: `pragma`s are implementation-specific features and usually only work with a given implementation. Every other implementation is allowed to silently ignore it, so not only will it not work, you might not be told it didn't work. Some `pragma`s prove generally useful and get wide adoption or are incorporated into the language itself, but to my knowledge only Visual Studio supports this particular `pragma`. If you are not using Visual Studio expect linker errors in the near future. – user4581301 Jul 25 '22 at 15:17
  • In my version of `ws2tcpip.h`, `getnameinfo()` (and `getaddrinfo()` and `freeadrinfo()`) is not protected by any versioning macros, but `GetNameInfo()`/`GetNameInfoA()`/`GetNameInfoW()` are protected by `#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)`. On the other hand, `inet_ntop()` is protected by `#if (NTDDI_VERSION >= NTDDI_VISTA)` – Remy Lebeau Jul 25 '22 at 19:37

0 Answers0