好像平时SDK的基于消息写法。rc = WSAAsyncSelect(s, hWnd, wMsg, FD_READ|FD_WRITE);然后可以用wMsg消息带来的两个参数,标识事件和套接字。CAsyncSocket里边怎么实现呢?

解决方案 »

  1.   

    wMsg就是你自己的消息吧,在前面自己define一下就可以了吖~~
      

  2.   


    CAsyncSocket 这个类没有相关的消息注册?
      

  3.   


    貌似当有消息来之后就会发送你自己定义的消息,通过消息的参数可以划分 FD_READ ¦FD_WRITE等事件..
      

  4.   

    CAsyncSocket::Create(
       UINT nSocketPort = 0,
       int nSocketType = SOCK_STREAM,
       long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE,
       LPCTSTR lpszSocketAddress = NULL 
    );
    冠希, 你又得给分我了~:) 多看看msdn
      

  5.   

    static void PASCAL CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
    {
               CAsyncSocket Socket;
               Socket.Attach( (SOCKET)wParam ); //wParam就是触发这个事件的Socket的句柄
               int nErrorCode = WSAGETSELECTERROR(lParam); //lParam是错误码与事件码的合成
               switch (WSAGETSELECTEVENT(lParam))
               {
                     case FD_READ:
                         pSocket->OnReceive(nErrorCode);
                         break;
                     case FD_WRITE:
                         pSocket->OnSend(nErrorCode);
                         break;
                     case FD_OOB:
                         pSocket->OnOutOfBandData(nErrorCode);
                         break;
                     case FD_ACCEPT:
                         pSocket->OnAccept(nErrorCode);
                         break;
                    case FD_CONNECT:
                         pSocket->OnConnect(nErrorCode);
                         break;
                    case FD_CLOSE:
                         pSocket->OnClose(nErrorCode);
                         break;
                }
       }
      

  6.   


    这个DoCallBack怎么用?怎么在成员里边是没有的?怎么用呢?看见这个就好了。但是类里没说有这个...怎么用。CAsyncSocket Constructs a CAsyncSocket object. 
    Create Creates a socket. 
    AttributesAttach Attaches a socket handle to a CAsyncSocket object. 
    Detach Detaches a socket handle from a CAsyncSocket object. 
    FromHandle Returns a pointer to a CAsyncSocket object, given a socket handle. 
    GetLastError Gets the error status for the last operation that failed. 
    GetPeerName Gets the address of the peer socket to which the socket is connected. 
    GetSockName Gets the local name for a socket. 
    GetSockOpt Retrieves a socket option. 
    SetSockOpt Sets a socket option. 
    OperationsAccept Accepts a connection on the socket. 
    AsyncSelect Requests event notification for the socket. 
    Bind Associates a local address with the socket. 
    Close Closes the socket. 
    Connect Establishes a connection to a peer socket. 
    IOCtl Controls the mode of the socket. 
    Listen Establishes a socket to listen for incoming connection requests. 
    Receive Receives data from the socket. 
    ReceiveFrom Receives a datagram and stores the source address. 
    Send Sends data to a connected socket. 
    SendTo Sends data to a specific destination. 
    ShutDown Disables Send and/or Receive calls on the socket. 
    Overridable Notification FunctionsOnAccept Notifies a listening socket that it can accept pending connection requests by calling Accept. 
    OnClose Notifies a socket that the socket connected to it has closed. 
    OnConnect Notifies a connecting socket that the connection attempt is complete, whether successfully or in error. 
    OnOutOfBandData Notifies a receiving socket that there is out-of-band data to be read on the socket, usually an urgent message. 
    OnReceive Notifies a listening socket that there is data to be retrieved by calling Receive. 
    OnSend Notifies a socket that it can send data by calling Send. 
    Data Membersm_hSocket Indicates the SOCKET handle attached to this CAsyncSocket object.  
      

  7.   


    是有,但在那里可以判断FD_这个值呢?类成员真的没发现相关的呀。只有On什么什么的一些通知,但没有wParam、lParam这参数。实在不懂啊。
      

  8.   

    需要吗?CAsyncSocket的消息通过重载虚函数(就是On什么什么的)就实现了消息处理了。
      

  9.   

    请问On什么的时候,带来的lParam和wParam的参数怎么得到。好像它有消息来到的时候,有一个socket,标识着那个客户连接过来,怎么办?
      

  10.   

    服务器端的话,在OnAccept中用Accept建立连接,这时候Accept中的参数就是连接到客户端的Socket,自己保管。
      

  11.   


    这个OnAccept是不是对应API编程中的FD_ACCEPT这个事件?当我又调用Accept的时候,会接受到一个,保存起来?下一次来的是新的,与新的联系起来?