关于网络编程里的select()函数我一直没搞明白是做什么的?
看网上说了很多。但就是没明白。
谁能给点容易懂的说法?
MSDN上的没看懂,英语能力有限。

解决方案 »

  1.   

    你指的是SQL里面的查询语言吗?
    Select * From Table where id = ..;
    其中的*代表数据库表里面的选项,Table表示表的名称,id=..代表是查询的条件。
    写好了Sql语句,就可以来执行了。
    你可以找本网络编程方面的书好好看看,应该都有好很详细的解释的!
      

  2.   

    《windows网络通信与程序设计》第3章中对select讲解得非常清楚
      

  3.   

    Res
    The select function is used to determine the status of one or more sockets. For each socket, the caller can request information on read, write, or error status. The set of sockets for which a given status is requested is indicated by an fd_set structure. The sockets contained within the fd_set structures must be associated with a single service provider. For the purpose of this restriction, sockets are considered to be from the same service provider if the WSAPROTOCOL_INFO structures describing their protocols have the same providerId value. Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition. The select function returns the number of sockets meeting the conditions. A set of macros is provided for manipulating an fd_set structure. These macros are compatible with those used in the Berkeley software, but the underlying representation is completely different.The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be ed as readable if an incoming connection request has been received such that an accept is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block.For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. If the virtual circuit was reset, then a recv will complete immediately with an error code such as WSAECONNRESET. The presence of OOB data will be checked if the socket option SO_OOBINLINE has been enabled (see setsockopt).The parameter writefds identifies the sockets that are to be checked for writability. If a socket is processing a connect call (nonblocking), a socket is writeable if the connection establishment successfully completes. If the socket is not processing a connect call, writability means a send, sendto, or WSASendto are guaranteed to succeed. However, they can block on a blocking socket if the len parameter exceeds the amount of outgoing system buffer space available. It is not specified how long these guarantees can be assumed to be valid, particularly in a multithreaded environment.The parameter exceptfds identifies the sockets that are to be checked for the presence of OOB data or any exceptional error conditions.Note  Out-of-band data will only be reported in this way if the option SO_OOBINLINE is FALSE. If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred). This document does not define which other errors will be included.Any two of the parameters, readfds, writefds, or exceptfds, can be given as null. At least one must be non-null, and any non-null descriptor set must contain at least one handle to a socket.In summary, a socket will be identified in a particular set when select returns if:readfds:
    If listen has been called and a connection is pending, accept will succeed. 
    Data is available for reading (includes OOB data if SO_OOBINLINE is enabled). 
    Connection has been closed/reset/terminated. 
    writefds:
    If processing a connect call (nonblocking), connection has succeeded. 
    Data can be sent. 
    exceptfds:
    If processing a connect call (nonblocking), connection attempt failed. 
    OOB data is available for reading (only if SO_OOBINLINE is disabled). 
    Four macros are defined in the header file Winsock2.h for manipulating and checking the descriptor sets. The variable FD_SETSIZE determines the maximum number of descriptors in a set. (The default value of FD_SETSIZE is 64, which can be modified by defining FD_SETSIZE to another value before including Winsock2.h.) Internally, socket handles in an fd_set structure are not represented as bit flags as in Berkeley Unix. Their data representation is opaque. Use of these macros will maintain software portability between different socket environments. The macros to manipulate and check fd_set contents are:
    FD_CLR(s, *set) 
    Removes the descriptor s from set.FD_ISSET(s, *set) 
    Nonzero if s is a member of the set. Otherwise, zero.FD_SET(s, *set) 
    Adds descriptor s to set.FD_ZERO(*set) 
    Initializes the set to the null set.The parameter time-out controls how long the select can take to complete. If time-out is a null pointer, select will block indefinitely until at least one descriptor meets the specified criteria. Otherwise, time-out points to a TIMEVAL structure that specifies the maximum time that select should wait before returning. When select returns, the contents of the TIMEVAL structure are not altered. If TIMEVAL is initialized to {0, 0}, select will return immediately; this is used to poll the state of the selected sockets. If select returns immediately, then the select call is considered nonblocking and the standard assumptions for nonblocking calls apply. For example, the blocking hook will not be called, and Windows Sockets will not yield.Note  The select function has no effect on the persistence of socket events registered with WSAAsyncSelect or WSAEventSelect.msdn上抄的,自己先看看啊。还看不懂的话看看这个http://www.360doc.com/content/09/1220/19/509553_11593797.shtml
      

  4.   

    原理:首先初始化一个套接字集合fdSocket,并把监听套接字,客户套接字都加入其中,用select函数进行等待,当有事件发生时进行相应的处理。
    下面的代码写的很详细://////////////////////////////////////////////////////
    // select.cpp文件
    #include "../common/initsock.h"
    #include <stdio.h>CInitSock theSock; // 初始化Winsock库
    int main()
    {
    USHORT nPort = 4567; // 此服务器监听的端口号 // 创建监听套节字
    SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(nPort);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    // 绑定套节字到本地机器
    if(::bind(sListen, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
    printf(" Failed bind() \n");
    return -1;
    }
    // 进入监听模式
    ::listen(sListen, 5); // select模型处理过程
    // 1)初始化一个套节字集合fdSocket,添加监听套节字句柄到这个集合
    fd_set fdSocket; // 所有可用套节字集合
    FD_ZERO(&fdSocket);
    FD_SET(sListen, &fdSocket);
    while(TRUE)
    {
    // 2)将fdSocket集合的一个拷贝fdRead传递给select函数,
    // 当有事件发生时,select函数移除fdRead集合中没有未决I/O操作的套节字句柄,然后返回。
    fd_set fdRead = fdSocket;
    int nRet = ::select(0, &fdRead, NULL, NULL, NULL);
    if(nRet > 0)
    {
    // 3)通过将原来fdSocket集合与select处理过的fdRead集合比较,
    // 确定都有哪些套节字有未决I/O,并进一步处理这些I/O。
    for(int i=0; i<(int)fdSocket.fd_count; i++)
    {
    if(FD_ISSET(fdSocket.fd_array[i], &fdRead))
    {
    if(fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接
    {
    if(fdSocket.fd_count < FD_SETSIZE)
    {
    sockaddr_in addrRemote;
    int nAddrLen = sizeof(addrRemote);
    SOCKET sNew = ::accept(sListen, (SOCKADDR*)&addrRemote, &nAddrLen);
    FD_SET(sNew, &fdSocket);
    printf("接收到连接(%s)\n", ::inet_ntoa(addrRemote.sin_addr));
    }
    else
    {
    printf(" Too much connections! \n");
    continue;
    }
    }
    else
    {
    char szText[256];
    int nRecv = ::recv(fdSocket.fd_array[i], szText, strlen(szText), 0);
    if(nRecv > 0) // (2)可读
    {
    szText[nRecv] = '\0';
    printf("接收到数据:%s \n", szText);
    }
    else // (3)连接关闭、重启或者中断
    {
    ::closesocket(fdSocket.fd_array[i]);
    FD_CLR(fdSocket.fd_array[i], &fdSocket);
    }
    }
    }
    }
    }
    else
    {
    printf(" Failed select() \n");
    break;
    }
    }
    return 0;
    }