How can I resolve a host IP address, given a URL in Visual C++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
The IP address will be in
he->h_addr
. Works on both windows, linux and most likely macos.Parse the URL to get the host name. Then call gethostbyname or the corresponding API on your platform to get the IP address(es). If you are parsing a HTTP header, look for the HostName header to determine the host name.
gethostbyname
?To use the socket functions under Windows, you have to start by calling
WSAStartup
, specifying the version of Winsock you want (for your purposes, 1.1 will work fine). Then you can callgethostbyname
to get the address of the host. When you're done, you're supposed to call WSACleanup. Putting that all together, you get something like this:I'm not sure if there is a specific C++ class to do host name lookups, but you can always resort to plain C for such things. Here's my version which compiles and runs on Linux, Mac OS X, and Windows.
Once compiled, provide the host name as an argument: