Game Development Reference
In-Depth Information
minor version number in the high order byte and the major version in the low order
byte. If for some reason you wanted to initialize Windows Sockets version 2.0, you ' d
send 0x0002 into the WSAStartup() function. As you can see below, you can also
use the MAKEWORD macro to set the version number properly.
WORD wVersionRequested = MAKEWORD( 0, 2 );
// set to 2.0
WSADATA wsaData;
int err = WSAStartup( wVersionRequested, &wsaData );
WSAStartup() also takes a pointer to the WSADATA structure. This structure is filled
with data that describes the socket implementation and its current status, but that
'
s
about it.
WSACleanup() is called when you are ready to shut down your application.
Creating Sockets and Setting Socket Options
The embodiment of a socket is the socket handle. You should already be familiar
with using handles from working with resources in the resource cache. The difference
comes in the multistep manner in which you create a connected socket. The easiest
connection style is a client-side connection. Doing this requires three steps. First, you
ask the sockets API to create a socket handle of a particular type. You have the
option of changing socket options, which tells the sockets API more information
about how you want the socket to act. After that, you can connect the socket with
another computer. It is a little more involved than opening a file, but sockets are a
little more complicated.
socket()
The following is the API to create a socket, interestingly enough:
SOCKET socket (int address_family, int socket_type, int protocol );
Parameters:
n Address Family: Will always be PF_INET for communicating over the Internet
using IPv4. Other address families include PF_IPX , PF_DECnet , PF_APPLE-
TALK , PF_ATM , and PF_INET6 .
n Socket Type: Use SOCK_STREAM for connected byte streams. SOCK_DGRAM is
for connectionless network communication, and SOCK_RAW is for raw sockets,
which lets you write socket programs at a lower level than TCP or UDP. You
will generally use SOCK_STREAM .
n Protocol: Use IPPROTO_TCP for TCP and IPPROTO_UDP for UDP sockets.
 
 
Search WWH ::




Custom Search