Sign in to follow this  
ironhead

about gethostbyname in winsock

Recommended Posts

ironhead    122
I have to program with Winsock recently, and have a problem when using function gethostbyname. I try to get IP by calling the function with domain name like this : gethostbyname("D2004.ddns.lala.com"); then I could get an IP Address. but this one is IP of a ddns server. How can I get the real IP of the server ? Should I try to contact with ddns server or Is there any function work for this purpose ?? Thanks for any help : )

Share this post


Link to post
Share on other sites
hplus0603    11356
Just call gethostbyname() on whatever the actual name of the server is.

If the actual name of the server is D2004.ddns.lala.com, then whatever is returned by that query, is the record for that server name, and the address you should use. If that doesn't work, then something's wrong on the DDNS side, not in gethostbyname().

Share this post


Link to post
Share on other sites
ironhead    122
Thanks for your reply, I try again and again but it still not work correctly.

This domain name is what I use for testing my program :
D7501.ddns.iview-ddns.com

Using gethostbyname() I will get ip "211.75.21.14", but the real ip I should get is "202.168.200.151".
Its strange because when using IE to connect http::\\D7501.ddns.iview-ddns.com, IE will connect to 202.168.200.151, it work perfectly. And this is why I still try to find another method.

Here is my simple testing code:

bool SocketStartUp(void)
{
WSADATA wsaData;

int Status = WSAStartup(MAKEWORD(2, 0), &wsaData);
if(Status)
return false;
return true;
}

bool SocketShutDown(void)
{
if(WSACleanup() == SOCKET_ERROR)
return false;
return true;
}

int main(int argc, char** argv)
{
if(SocketStartUp())
std::cout << "Start Success !!!" << std::endl;
else
{
std::cout << "Start Failed !!!" << std::endl;
return 1;
}

LPHOSTENT lphost(0);
lphost = gethostbyname("D7501.ddns.iview-ddns.com");
if(lphost)
{
in_addr addr;
std::string name;

addr.S_un.S_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
name = inet_ntoa(addr);
std::cout << name << std::endl;

std::cout << lphost->h_addr_list[0] << std::endl;
}

SocketShutDown();
}

Thanks for any Help.

Share this post


Link to post
Share on other sites
hplus0603    11356
IE will use proxying and redirects to get to the final destination.

If you "ping" that location, where will you end up?

If you run Ethereal on the session where IE connects to that URL (forward slashes, btw!), what happens on the wire?

Share this post


Link to post
Share on other sites
ironhead    122
Thank you very much!!!

I have resolve this problem,
and my program could work correctly now.
After using the tool "Ethereal",
I send some message to server like IE does,
then the message that server replied have real IP I need.

Thank you for mentioning this helpful Tool.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this