Bonjour,
Récemment, j’ai changé d’IDE, de Visual Studio à gcc (command line). J’avais fais un petit programme winsock pour ’winsocker’..
Le code marchait parfaitement sur visual studio, par contre, en le compilant avec gcc :
gcc .\nom_du_programme.cpp
sur windows power shell, j’obtient la belle erreur :
.\Winsock_Server.cpp: In function ’int main()’: .\Winsock_Server.cpp:84:58: error: ’inet_ntop’ was not declar inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
Si qulqu’un pouvait m’aider
C0de:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include <iostream> #include <WS2tcpip.h> #include <string> #pragma comment (lib, "ws2_32.lib") using std::cout; using std::endl; using std::string; int main() { //---Initialze Winsock--- WSADATA wsdata; int iResult = WSAStartup(MAKEWORD(2, 2), &wsdata); //MAKEWORD(2, 2) |> request version 2.2 of winsock if (iResult != 0){ cout << "Error at: Initializing Winsock.., Winsock 2.2 may be not supported by your system or simply not installed" << endl; return 1; } else { cout << "Winsock is working,\n" "Status : " << wsdata.szSystemStatus << endl; } //---Create a socket--- SOCKET listeningSock = socket(AF_INET, SOCK_STREAM, 0); if (listeningSock == -1) { cout << "Error at: Socket Error.. " << endl; closesocket(listeningSock); WSACleanup(); return 1; } //---Bind ip addr and port to a socket--- sockaddr_in hint; hint.sin_family = AF_INET;//IPv4 hint.sin_port = htons(54000);//Port hint.sin_addr.S_un.S_addr = INADDR_ANY;//All ip's can connect bind(listeningSock, (sockaddr*)&hint, sizeof(hint)); //---Tell Winsock socket is for listening--- listen(listeningSock, SOMAXCONN); //---Wait for connection--- sockaddr_in client; int clientsize = sizeof(client); SOCKET ClientSocket = accept(listeningSock, (sockaddr*)&client, &clientsize) ; if (ClientSocket == INVALID_SOCKET) { cout << "Error at: Connection.. " << wsdata.szSystemStatus << endl; } char host[NI_MAXHOST]; char service[NI_MAXHOST]; ZeroMemory(host, NI_MAXHOST); ZeroMemory(service, NI_MAXHOST); if (getnameinfo((sockaddr*)&client, sizeof(client), host,NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { cout << host << " is connected on port " << service << endl; } else { inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);//<-----------------ICICICI!!! cout << host << " is connected on port " << ntohs(client.sin_port) << endl; } //---Close listening socket--- closesocket(listeningSock); //---While loop: accept and echo message back to client (test)--- char buffer[4096]; while (true) { ZeroMemory(buffer, 4096); //Wait client send data int bytesReceived = recv(ClientSocket, buffer, 4096, 0); if (bytesReceived == SOCKET_ERROR) { cout << host << " is now Disconnected.. " << endl; break; } if (bytesReceived ==0) { cout << "Error at: recevieiving information.." << endl; } cout << (buffer, 0, bytesReceived) << endl; //Echo message back |> client send(ClientSocket, buffer, bytesReceived + 1, 0); //Echo client message |> serv console cout << "["<< host << "]"<< "[+]" << buffer << endl; } //---Close socket--- closesocket(ClientSocket); //---Clean up Winsock--- WSACleanup(); system("pause"); return 0;//Cyka Blyt } ` |
+0
-0