Game Development Reference
In-Depth Information
Sockets Utility Functions
There are some useful conversion functions that help you deal with Internet
addresses and data that has been sent by another computer. The first two functions,
inet_addr() and inet_ntoa() , perform conversions from a text string dotted
decimal IP address and the four-byte unsigned integer. You
'
ll notice the input
parameter for inet_ntoa() is a structure called in_addr :
Takes a string value like 127.0.0.1 and con-
verts it to an unsigned integer you can use as
an IP address.
unsigned long inet_addr(
const char* cp
);
Takes an in_addr structure and converts it to a
string. Note: Copy the string from the return
pointer; don
char* FAR inet_ntoa(
struct in_addr in
t assume it will be there for long.
It points to a static char buffer and may be
overwritten the next time a socket
'
);
'
s function is
called.
The in_addr structure is something that helps you break up IP addresses into their
component parts. It
s not just a normal unsigned integer, because the values of the
bytes are in a specific order. This might seem confusing until you recall that different
machines store integers in Big-endian or Little-endian order. In a Big-endian system,
the most significant value in the sequence is stored at the lowest storage address (for
example,
'
). In a Little-endian system, the least significant value in the
sequence is stored first. Take a look at how the two systems store the 4-byte integer
0x80402010:
big end first
Big-endian
80 40 20 10
Little-endian
10 20 40 80
They are exactly backward from each other. Intel processors use Little-endian, and
Motorola processors use Big-endian. The Internet standard is Big-endian. Some pro-
cessors such as ARM and PowerPC are actually bi-endian and have the ability to
switch between the two, typically on startup. This means that you have to be really
careful with the data you get from strange computers because it might be in the
wrong order. For certain sockets data structures, you are also expected to put things
in network order. Luckily, there are some helper functions for that.
 
 
Search WWH ::




Custom Search