Jump to content
  • Advertisement
Sign in to follow this  
Dookie

Help using gethostbyname()...

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again! I got another easy one for you... I'm trying to use 'gethostbyname()' so users can either type in an IP address or a computer/domain name. Well, it works all the way up to where I want to put the resulting info into connect(). Let's say I have this: hostent* remoteHost; and I'm using this, where 'host_name' is the name of a computer: remoteHost = gethostbyname(host_name); What do I put in this (where 'client_in' is a sockaddr_in struct)? client_in.sin_addr.s_addr I need a valid 's_addr' in order to do a connect(), and I don't know how to get that address from a computer or domain name. Would anyone help me on this? Thanks in advance for the help!

Share this post


Link to post
Share on other sites
Advertisement
Easier question...

How do I get something I can put in this:

client_in.sin_addr.s_addr

if all I have is the server computer's name? I can get it to work fine if I have the server's IP address, but I'd like this to work if I can get a value for this when all I have is the computer's name.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dookie
Hello again! I got another easy one for you...

I'm trying to use 'gethostbyname()' so users can either type in an IP address or a computer/domain name. Well, it works all the way up to where I want to put the resulting info into connect().

Let's say I have this:

hostent* remoteHost;

and I'm using this, where 'host_name' is the name of a computer:

remoteHost = gethostbyname(host_name);

What do I put in this (where 'client_in' is a sockaddr_in struct)?

client_in.sin_addr.s_addr

I need a valid 's_addr' in order to do a connect(), and I don't know how to get that address from a computer or domain name. Would anyone help me on this?

Thanks in advance for the help!


If I am not mistaken:

memcpy(&client_in.sin_addr, hostent->h_addr, hostent->h_length);

[Edited by - snisarenko on July 26, 2006 8:34:50 PM]

Share this post


Link to post
Share on other sites
Thanks for the reply, snisarenko. I tried what you suggested, but it didn't work... What I'm doing is almost exactly what you see on this website:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp

Look at it and you'll see at the bottom of the webpage that "the remoteHost structure can now be used to access information about the host". But it doesn't say how to get that info. So I'm left trying to figure out how to get the website's IP address into my sockaddr_in struct, and am having pretty lousy luck with it. [sad] Msdn also says to use 'getaddrinfo' instead of 'gethostbyname', but my programming environment is too old and it doesn't support the newer 'getaddrinfo' function.

Has anybody else had any luck getting this to work?

Share this post


Link to post
Share on other sites
OK, I got it to work. Turned out I had to do some obscure thing to extract the IP address from the 'hostent' struct...

struct hostent *remoteHost;
...
inet_ntoa( *( struct in_addr*)( remoteHost->h_addr))


That 'inet_ntoa' turns the IP address info into a string, so all I needed to do was copy its contents into a character array called 'ip_Text'. Then I plugged it into my sockaddr_in struct like this:

client_in.sin_addr.s_addr = inet_addr (ip_Text);

Pretty weird, but it works. Actually it's all weird to me, but I'm still pretty new to all this sockets-n-stuff.

Thanks for the help! I'll probably post something more challenging in the future, so stay tuned! [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!