windows equivalent of inet_aton

24,358

Solution 1

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).

Solution 2

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.

Solution 3

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
Share:
24,358
SSS
Author by

SSS

Updated on June 28, 2020

Comments

  • SSS
    SSS almost 4 years

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

  • SSS
    SSS about 14 years
    Once I use inet_addr and set the S_addr member of the in_addr struct with the return value, what are the other two union members of the struct in_addr? I'm not quite sure what these (S_un_b and S_un_w) need to be set to.
  • SSS
    SSS about 14 years
    I assuming I can just use the sockaddr struct type as opposed to the sockaddr_in type and therefore will not need to worry about S_un_b and S_un_w....although I would still like to know what they are used for. Thanks.
  • SSS
    SSS about 14 years
    Also I don't know why I overlooked the fact that it is a union. I guess no further explanations are needed.
  • Steve Jessop
    Steve Jessop about 14 years
    The other members of the union are for if you want to manipulate the address somehow, so that you can look at it byte by byte. On Windows this makes sense because Windows is always little-endian. You rarely need to do that, though: the most common manipulation I guess would be to apply a netmask, and you don't need byte access for that.
  • Vi.
    Vi. over 13 years
    But it is only from Vista and above. on Windows XP it will fail to load.