简体   繁体   English

Windows XP 中是否有 inet_ntop / InetNtop 的替代方案?

[英]Is there an alternative to inet_ntop / InetNtop in Windows XP?

I'm trying to compile beej's guide to network programming examples, but Windows XP doesn't have such a function.我正在尝试编译 beej 的网络编程示例指南,但 Windows XP 没有这样的功能。 I'm using mingw, if it makes any difference.我正在使用 mingw,如果它有什么不同的话。

From the WinSock layer:从 WinSock 层:

If you're only dealing with IPv4 addresses, you can use inet_ntoa .如果您只处理 IPv4 地址,则可以使用inet_ntoa It's available on Windows 2000 or later.它在 Windows 2000 或更高版本上可用。 Otherwise you'll have to either require Vista and later, or write your own inet_ntop function.否则,您将不得不要求 Vista 及更高版本,或者编写您自己的 inet_ntop 函数。

You could also look at boost - the boost::asio has an inet_ntop implementation that works in Windows: boost::asio::detail::socket_ops::inet_ntop .您还可以查看 boost - boost::asio 有一个适用于 Windows 的inet_ntop实现: boost::asio::detail::socket_ops::inet_ntop You can see the source code here .您可以在此处查看源代码。

There is also inet_ntop function in POSIX compliant libc for Windows (PlibC) library that was created for porting POSIX applications to Windows. POSIX 兼容的 libc for Windows (PlibC)库中还有inet_ntop函数, inet_ntop函数是为将 POSIX 应用程序移植到 Windows 而创建的。 There is no notes about it in online documentation , but it exists in file inet_ntop.c at least since 2008 (according to file date).在线文档中没有关于它的注释,但它至少从 2008 年开始就存在于文件inet_ntop.c中(根据文件日期)。

const char * inet_ntop(int af, const void *src, char *dst, size_t size)

you can use winsocket with mingw-64 on windows 7您可以在 Windows 7 上使用带有 mingw-64 的 winsocket

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>

linkwith链接

gcc showip.c -lws2_32

Target: x86_64-w64-mingw32 Thread model: win32 gcc version 6.3.0 (GCC)目标:x86_64-w64-mingw32 线程模型:win32 gcc version 6.3.0 (GCC)

You might want to use something Jeroen Massar provided here , excerpt from his post follows:您可能需要使用的东西的Jeroen Massar提供在这里,从他的岗位摘录如下:

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
    if (af == AF_INET)
    {
        struct sockaddr_in in;
        memset(&in, 0, sizeof(in));
        in.sin_family = AF_INET;
        memcpy(&in.sin_addr, src, sizeof(struct in_addr));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    else if (af == AF_INET6)
    {
        struct sockaddr_in6 in;
        memset(&in, 0, sizeof(in));
        in.sin6_family = AF_INET6;
        memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    return NULL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM