if (getnameinfo((sockaddr *) & client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
std::cout << host << " Connected on port " << service << std::endl;
}
else
{
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
std::cout << host << " Connected on port " << ntohs(client.sin_port) << std::endl;
}
I hoped to get "host" connected on port, but instead I get the error:
'inet_ntop' was not declared in this scope
I have tried to add more #include
statements, which didn't work. I think I get the error because I maybe have an outdated something?
EDIT : I need to do a minimal reproducible example , but my English is very bad, so it would take me a year to learn/read, so I'm gonna find help another place :-)
Make sure you have #include <ws2tcpip.h>
in your code.
But also note that inet_ntop()
was added to WinSock in Windows Vista, so make sure that your project is configured to target Vista or later, as ws2tcpip.h
declares inet_ntop()
only if NTDDI_VERSION >= NTDDI_VISTA
(0x06000000) (see Using the Windows Headers and Update WINVER and _WIN32_WINNT ).
If targeting Vista+ at the project/compiler level is not an option (ie, you need to target XP, too), then you can instead import inet_ntop()
via GetProcAddress()
at runtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.