[gameprogrammer] Re: "Network Byte Order" IP's to dotted-triplet format

On Wed, 2004-11-10 at 14:58, Jake The Snake Briggs wrote:
> Hi all
> I need to get the IP address of a socket. I am writing a multiplayer 
> game, basicly its "snake", but multiplayer. I want to write 3 programs, 
> a client game, a server, and a master server. I want to be able to tell 
> the master server to give me a list of all the game servers that are 
> registered with it. So far i have a crappy game server written, that can 
> register with the master server. I have a really crappy game client 
> written. I have the simple master sever 95% finished, and thats where my 
> problem lies.
> I am using this function :
> 
> IPaddress *NET2_TCPGetPeerAddress(int socket);
> 
> because i am using Bob Pendletons NET2_SDL library. This fills the 
> specified IPaddress struct with the IP and Port of the socket in 
> "Network Byte Order" format. So i think (after a bit of googleing) i 
> understand what that is, and why its used.
> At the moment i can go
> telnet <masterserverip> <masterserverport>
> then type
> "servers"
> and i get back a list of server ip's and ports. But I want them to be 
> the dotted triplet format, like 192.168.0.2 rather than 18328373673 or 
> whatever it is. How would one go about transforming Network Byte Order 
> to dotted format?

The content of the host item in the IPaddress structure is just an
unsigned integer. First, convert it to host order using ntohl(). Then
assuming the value is in "a" you use something like:

printf("%d.%d.%d.%d\n", (a >> 24) & 0xff,
                        (a >> 16) & 0xff, 
                        (a >> 8) & 0xff, 
                        a & 0xff);

And, yeah, all the OxFF constants are need. Never trust the compiler not
to fill in high order 1s when shifting left.

                Bob Pendleton

> 
> 
> 
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
> 
-- 
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob@xxxxxxxxxxxxx             +
+ blog:  www.Stonewolf.net             +
+ web:   www.GameProgrammer.com        +
+--------------------------------------+



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: