Game Development Reference
In-Depth Information
setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&value, sizeof(value));
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&value, sizeof(value));
The first option, TCP_NODELAY , disables an internal buffering mechanism in an
attempt to sacrifice some network bandwidth for a speedier sending of packets. It is
especially important when you want to send a high number of small packets, as is
common in many multiplayer computer games.
The next option, SO_DONTLINGER , ensures a speedy return from a call to close the
socket. The socket will be closed gracefully, but the call will essentially happen in the
background. This is a clear win for any application that has to support a high num-
ber of connections, but it is still good for a computer game, no matter how many
connections you have.
The last one of interest is SO_KEEPALIVE . It sends a packet of data at regular inter-
vals if no other data has been sent. The default interval is two hours, but on some
systems it can be configurable. This is probably only useful for a server system that
supports a high number of connections. In a multiperson shooter, it will be pretty
obvious if someone
'
s remote connection goes dark.
ioctlsocket()
Another useful socket control function is ioctlsocket() , which has a few uses, but
the most important one to you, the fledgling multiplayer game programmer, is to set
whether a socket is a blocking socket or a nonblocking socket:
int ioctlsocket( SOCKET s, long command, u_long* argumentPointer );
Parameters:
n Socket: A valid socket handle.
n Command: FIONBIO controls blocking. FIONREAD will return the number of
bytes ready in the socket
s input buffer, and SIOCATMARK will tell you if there is
any out-of-band (OOB) data to be read. OOB data is only available for sockets
that have the SO_OOBINLINE socket options set.
n Argument Pointer: A pointer to a u_long that holds the argument to the
command or stores the result of the command.
'
Return Value:
Returns zero if the option was set or SOCKET_ERROR if there was an error.
A blocking socket is one that will wait to send or receive data. A nonblocking socket
performs these tasks asynchronously. When you call the socket
'
s function to receive
data on a blocking socket, it won ' t return until there is actually data to receive.
Search WWH ::




Custom Search