Message-ID: <4BPi6K$w5z@bbs.sdu.edu.cn>
Subject: select i/o model (4)
Date: Sun, 28 Mar 2004 11:55:32 +0000
From: onek.bbs@bbs.sdu.edu.cn (�式�)
Path: news.cnusenet.org!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-cn.bbs.comp.lang.mbox.zip!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!news.moat.net!ctu-peer!news.nctu.edu.tw!netnews.csie.nctu.edu.tw!maily.cic.tsinghua.edu.cn!netnews.sdu.edu.cn!SDU
Newsgroups: cn.bbs.comp.lang
Organization: 泉������
X-Filename: Programming/M.1080474932.A
Xref: news.cnusenet.org cn.bbs.comp.lang:18072


Before you can begin to use select to monitor sockets, your application has to
 set up either one or all of the read, write, and exception fd_set structures 
by assigning socket handles to a set. When you assign a socket to one of the s
ets, you are asking select to let you know if the I/O activities just describe
d have occurred on a socket. Winsock provides the following set of macros to m
anipulate and check the fd_set sets for I/O activity.

FD_ZERO(*set) Initializes set to the empty set. A set should always be cleared
 before using.

FD_CLR(s, *set) Removes socket s from set.

FD_ISSET(s, *set) Checks to see if s is a member of set and returns TRUE if so


FD_SET(s, *set) Adds socket s to set.

For example, if you want to find out when it is safe to read data from a socke
t without blocking, simply assign your socket to the fd_read set using the FD_
SET macro and then call select. To test whether your socket is still part of t
he fd_read set, use the FD_ISSET macro. The following five steps describe the 
basic flow of an application that uses select with one or more socket handles:


Initialize each fd_set of interest by using the FD_ZERO macro.

Assign socket handles to each of the fd_set sets of interest by using the FD_S
ET macro.

Call the select function and wait until I/O activity sets one or more of the s
ocket handles in each fd_set set provided. When select completes, it returns t
he total number of socket handles that are set in all of the fd_set sets and u
pdates each set accordingly.

Using the return value of select, your application can determine which applica
tion sockets have I/O pending by checking each fd_set set using the FD_ISSET m
acro.

After determining which sockets have I/O pending in each of the sets, process 
the I/O and go to step 1 to continue the select process.

When select returns, it modifies each of the fd_set structures by removing the
 socket handles that do not have pending I/O operations. This is why you shoul
d use the FD_ISSET macro as in step 4 to determine if a particular socket is p
art of a set. The following code sample outlines the basic steps needed to set
 up the select model for a single socket. Adding more sockets to this applicat
ion simply involves maintaining a list or an array of additional sockets.

SOCKET  s;
fd_set  fdread;
int     ret;

// Create a socket, and accept a connection

// Manage I/O on the socket
while(TRUE)
{
    // Always clear the read set before calling 
    // select()
    FD_ZERO(&fdread);

    // Add socket s to the read set
    FD_SET(s, &fdread);

    if ((ret = select(0, &fdread, NULL, NULL, NULL)) 
        == SOCKET_ERROR) 
    {
        // Error condition
    }

    if (ret > 0)
    {
        // For this simple case, select() should return
        // the value 1. An application dealing with 
        // more than one socket could get a value 
        // greater than 1. At this point, your 
        // application should check to see whether the 
        // socket is part of a set.

        if (FD_ISSET(s, &fdread))
        {
            // A read event has occurred on socket s
        }
    }
}
The advantage of using select is the capability to multiplex connections and I
/O on many sockets from a single thread. This prevents the explosion of thread
s associated with blocking sockets and multiple connections. The disadvantage 
is the maximum number of sockets that may be added to the fd_set structures. B
y default, the maximum is defined as FD_SETSIZE, which is defined in WINSOCK2.
H as 64. To increase this limit, an application might define FD_SETSIZE to som
ething large. This define must appear before including WINSOCK2.H. Also, the u
nderlying provider imposes an arbitrary maximum fd_set size, which typically i
s 1024 but is not guaranteed to be. Finally, for a large FD_SETSIZE, consider 
the performance hit of setting 1000 sockets before calling select followed by 
checking whether each of those 1000 sockets is set after the call returns.


--

�� ��源:��泉������ http://bbs.sdu.edu.cn [FROM: 202.194.32.132]