无法解决我的 TCP 服务器代码中的这个错误
Cannot solve this error in my TCP server code
所以,我试图编写这个简单的 TCP 服务器,但我遇到了这个错误
error: 'inet_ntop' was not declared in this scope; did you mean '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();
}
inet_ntop()
在运行时需要 Windows Vista 及更高版本,但您将 _WIN32_WINNT
设置为 0x501
,代表 Windows XP,因此声明<w32tcpip.h>
中的 inet_ntop()
在 compile-time 时被禁用,以防止程序在运行时无法启动。
您需要将 _WIN32_WINNT
至少设置为 0x600,而不是启用 inet_ntop()
。
但是,即使您解决了该问题,您的代码仍然无法编译,因为您在调用 inet_ntop()
时存在语法错误 - 第二个参数中的括号不平衡。您缺少 (
或错误的 )
,具体取决于您尝试使用以下哪种语法:
inet_ntop(AF_INET, &(client.sin_addr), host, NI_MAXHOST);
inet inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
您还有其他逻辑错误,这些错误在运行时才会出现。您没有对 bind()
、listen()
和 send()
进行任何错误处理。 send()
上也有潜在的缓冲区溢出。
所以,我试图编写这个简单的 TCP 服务器,但我遇到了这个错误
error: 'inet_ntop' was not declared in this scope; did you mean '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();
}
inet_ntop()
在运行时需要 Windows Vista 及更高版本,但您将 _WIN32_WINNT
设置为 0x501
,代表 Windows XP,因此声明<w32tcpip.h>
中的 inet_ntop()
在 compile-time 时被禁用,以防止程序在运行时无法启动。
您需要将 _WIN32_WINNT
至少设置为 0x600,而不是启用 inet_ntop()
。
但是,即使您解决了该问题,您的代码仍然无法编译,因为您在调用 inet_ntop()
时存在语法错误 - 第二个参数中的括号不平衡。您缺少 (
或错误的 )
,具体取决于您尝试使用以下哪种语法:
inet_ntop(AF_INET, &(client.sin_addr), host, NI_MAXHOST);
inet inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
您还有其他逻辑错误,这些错误在运行时才会出现。您没有对 bind()
、listen()
和 send()
进行任何错误处理。 send()
上也有潜在的缓冲区溢出。