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);