Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting IP and host from a socket.

Started by GeekPlusPlus Jun 4, 2004 at 1:29 AM 4 replies 400+ views
Original Post
GeekPlusPlus
GeekPlusPlus
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++
Newb Programmer: Geek++
Nervo
Nervo
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]
Well, R2D22U2..
GeekPlusPlus
GeekPlusPlus
hrm accept() says it can''t convert sockaddr to a sockaddr_in, and it seems to not want to typcast.

- Newb Programmer: Geek++
Newb Programmer: Geek++
Nervo
Nervo
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]
Well, R2D22U2..
GeekPlusPlus
GeekPlusPlus
Ah ha! I got it.

Thanks.



- Newb Programmer: Geek++
Newb Programmer: Geek++
hplus0603
hplus0603
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).
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.