开发者

windows equivalent of inet_aton

开发者 https://www.devze.com 2022-12-23 00:44 出处:网络
I\'m converting some code written for a linux system to a windows system.I\'m using C开发者_StackOverflow++ for my windows system and wanted to know the equivalent of the function inet_aton.Windows su

I'm converting some code written for a linux system to a windows system. I'm using C开发者_StackOverflow++ for my windows system and wanted to know the equivalent of the function inet_aton.


Windows supports inet_pton, which has a similar interface to inet_aton (but that works with IPV6 addresses too). Just supply AF_INET as the first parameter, and it will otherwise work like inet_aton.

(If you can change the Linux source, inet_pton will also work there).


It's the Windows equivalent rather than the C++ equivalent, but probably you want inet_addr, which I believe predates inet_aton and which Windows supports.

http://msdn.microsoft.com/en-us/library/ms738563.aspx

That article also lists, in the "see also" section, the full set of verbosely-named functions to handle IPv6 addresses and so on.


To run in windows XP, you can try this checking:

#pragma comment(lib, "Ws2_32.lib")

sockaddr_in inaddr;

#ifdef _WIN32_WINNT 0x0501
    inaddr.sin_addr.s_addr =inet_addr("10.10.10.10"); //for XP
#else
    inet_pton(AF_INET, "10.10.10.10", &inaddr.sin_addr.s_addr); //for Vista or higher
#endif


Consider using WSAStringToAddressA instead on Windows XP (WSAStartup is also required). It supports 255.255.255.255 and 0.0.0.0. It does not support a IPv4 address string with a fields number less than 4. If WSAStringToAddressA failed (for example the number of fields of the IPv4 address string is less than 4), then call inet_addr as the fallback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号