Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

GeekPlusPlus

Getting IP and host from a socket.

This topic is 5278 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

Hey guys, I''ve got a Windows Async socket setup going, and I was wondering how I could output the IP address and or the Host Name from the socket information. It seems that sa_data[2][3][4] and [5] in sockaddr structure hold the IP address in char form (gah). Any easy way to get a char* or a straight int version? And is there a function for getting the host name that goes with the IP? Hope someone can help - Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Advertisement
try using
sockaddr_in theiraddr;

(you'll use that struct when you accept())

inet_ntoa(theiraddr.sin_addr);

that will return a char* that converts the int in sin_addr to a dotted decimal form.

EDIT: Also to address your second question, look up gethostbyaddr() on msdn as they give a great explanation on all options and its return struct HOSTENT.

Any further questions, feel free to post.

[edited by - nervo on June 4, 2004 2:49:28 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by GeekPlusPlus
hrm accept() says it can't convert sockaddr to a sockaddr_in, and it seems to not want to typcast.

- Newb Programmer: Geek++


Its supposed to be the other way around. You declare a sockaddr_in and then you typecast it to sockaddr in accept().

Or is this what you are doing already?

EDIT: I'm off to bed, so someone else should be along to help you out if you have further questions tonight.

[edited by - nervo on June 4, 2004 3:20:46 AM]

Share this post


Link to post
Share on other sites
Also check out getsockname() to get the local inet address of a socket, and getpeername() to get the (remote) inet address of a client (for connected sockets).

Share this post


Link to post
Share on other sites

  • 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!