Game Development Reference
In-Depth Information
Both functions return a pointer to a data structure, hostent . The data structure
stores information about the host, such as its name, IP address, and more. The struc-
ture is allocated and managed by the sockets system, so don
t do anything other than
read it. Notice the liberal sprinkling of network-to-host conversion functions.
The output of the code is this line:
'
Address of ftp.microsoft.com is 0xcf2e858c
Instead of using the gethostbyname() function, I could have used these lines and
used gethostbyaddr() :
unsigned int netip = inet_addr(
);
pHostEnt = gethostbyaddr((const char *)&netip, 4, PF_INET);
207.46.133.140
The DNS lookup functions make it easy for you to specify IP addresses in a human-
readable form, which is important for setting up a server IP address in an options file
or in a dialog box without getting out the calculator.
DNS Functions Failing?
You can call the conversion functions anytime you want, but the DNS lookup
functions will fail if you try to call them before you
'
ve initialized the sockets
API.
Sockets Initialization and Shutdown
Even if you are programming Berkeley-style sockets on a Windows machine, you
'
ll
call the Windows Sockets API to initialize the sockets system and shut it down:
Initializes the Sockets API; you must call it
before calling any other sockets function.
int WSAStartup(
WORD wVersionRequested,
LPWSADATA lpWSAData
);
Call this to deregister the application from
using sockets, usually in your application
cleanup code.
int WSACleanup(void);
In the first function, WSAStartup() , you send in the version number of the sockets
implementation you want. At this writing, the most recent version of sockets is ver-
sion 2.2, and it has been that way for years. Notice that you want to send in the
 
 
Search WWH ::




Custom Search