Game Development Reference
In-Depth Information
'
Blocking sockets are easier to program, but they aren
t nearly as useful in game pro-
gramming. Imagine using a blocking socket on a multiplayer game. Each client would
be completely stopped, frozen in place, until some data was received. A nonblocking
socket is the only way a game can continue processing anything in the same thread,
which is why it is used overwhelmingly over the blocking sort.
Here
'
s how you call the ioctlsocket() function to set your socket to nonblocking:
unsigned long val = 1; // 1=non blocking, 0=blocking
ioctlsocket(m_sock, FIONBIO, &val);
There
'
s one thing you should watch out for, however. You can only call this function
on a
socket, meaning that it is a client socket that has been connected to a
server or a server socket that is listening for clients.
live
Connecting Sockets to a Server and Understanding Ports
Once you have a socket handle and set the options with ioctlsocket() , the socket
will be ready to connect to another computer. For a socket to connect, the computer
accepting the connection must be listening for it. This differentiates server-side sock-
ets from client-side sockets, even though they all use the same SOCKET handle struc-
ture and they all use the same functions to send and receive data.
For now, imagine you are simply creating a socket to attach to something like an FTP
server, such as ftp.microsoft.com. Here you are, over a dozen pages into a networking
chapter, and I haven
t put it off any longer.
The designers of the Internet realized that computers on the Internet might have multi-
ple connections to other computers simultaneously. They facilitated this by adding ports
to the IP protocol. In addition to specifying an IP address of a computer, you must
specify a port as well. Ports can be numbered from 1 to 65535, where 0 is reserved.
Various client/server applications like FTP and Telnet use well-known port assignments,
which is simply an agreement that certain server applications will use certain ports.
Most popular server applications like Telnet and FTP use ports in the 0
'
t even mentioned ports yet. Well, I can
'
1024 range,
but new server applications, like those for common chat programs and multiplayer
games, use higher port numbers. For example, Doom used port 666
-
quite appropriate!
The choice of port is fairly arbitrary. The only real limitation is that you can ' thavetwo
different applications on the same server listening on the same port number.
If you are creating a server, it
t already
dominated by something else that everyone uses. There are plenty to go around,
and some quick searches on the Internet will give you plenty of current information
about which applications are using which port.
'
s up to you to choose a good port that isn
'
 
Search WWH ::




Custom Search