Game Development Reference
In-Depth Information
select()
The last server-side method is select() . This function lets you poll the state of all
your open sockets. You create three arrays of socket pointers that will be polled. The
first set will be polled for input, the second set for output, and the third set for excep-
tions. Here ' s the fd_set structure definition and the definition for select() .
typedef struct fd_set {
u_int fd_count;
SOCKET fd_array[FD_SETSIZE];
} fd_set;
int select(
int nfds,
fd_set* readfds,
fd_set* writefds,
fd_set* exceptfds,
const struct timeval* timeout );
Parameters:
n nfds : Ignored in WinSock; only included for compatibility with Berkeley sockets.
n readfds, writefds, exceptfds : The arrays of pointers to sockets to be
polled for input, output, and exceptions.
n timeout : A pointer to a timeout structure. Set it to NULL if you want
select() to block until something happens on one of the sockets, or set it to a
valid timeout structure with all zeros to make a quick poll.
Return Value:
Returns zero if the function timed out, SOCKET_ERROR if there was an error, or the
number of sockets contained within the structures that are ready to process.
This function is a real boon for the server-side programmer. It helps with servers that
have tons of client connections and you don
t want to block on any of them, whether
they are set to blocking or nonblocking. This function can tell your program which
sockets are ready to read from, write to, or have suffered an exception of some kind.
'
Maximum Client Connections Is 64 by Default
By default, the fd_set structure can hold 64 sockets. That size is defined as
FD_SETSIZE in the WINSOCK2.H header file. In C++, you can define your own
FD_SETSIZE , as long as it
s defined before the WINSOCK2 header file is included.
You can set this compiler #define in the command line or project properties. If it
is defined anywhere after #include WinSock2.h , it will break horribly.
'
 
Search WWH ::




Custom Search