#define CLI_MESSAGE WM_USER+101void CClient::ClientInit()
{
if(WSAAsyncSelect(m_hSocket,m_hWnd,CLI_MESSAGE,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
AfxMessageBox("error in select");
}LRESULT CChatDlg::OnClientMessage(WPARAM wParam,LPARAM lParam)
{
     ……
switch(lParam)
{
case FD_CONNECT:
……;
                  return 0;
case FD_READ:
……;
                  return 0;
case FD_WRITE:
return 0;
case FD_CLOSE:
return 0;
default:
output=(CEdit*)GetDlgItem(IDC_SHOWTEXT);
output->SetWindowText("an network error has occured,the connection is dropped");
closesocket(m_client.m_hSocket);
m_bInit=false;
return 0;
}
}在ClientInit()里,并没有弹出AfxMessageBox("error in select");这说明CLI_MESSAGE是FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT的一种,但是在OnClientMessage函数中switch总是跳转到default里?
我把lParam转换成CString用AfxMessageBox弹出,发现lParam并没有值。谢谢各位!

解决方案 »

  1.   

    WSAAsyncSelect 的返回值你查了?
    If the WSAAsyncSelect function succeeds, the return value is zero, provided that the application's declaration of interest in the network event set was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError成功返回0 否则返回 SOCKET_ERROR 就是-1 你确定你成功了? 你的AfxMessageBox("error in select")永远不会弹出来
      

  2.   

    switch(lParam)整个都判读不对#define CLI_MESSAGE WM_USER+101void CClient::ClientInit()
    {
    if(SOCKET_ERROR == WSAAsyncSelect(m_hSocket,m_hWnd,CLI_MESSAGE,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT))
    {
    AfxMessageBox("error in select");
    }
    }LRESULT CChatDlg::OnClientMessage(WPARAM wParam,LPARAM lParam)
    {
    SOCKET s = wParam;
    if(WSAGETSELECTERROR(lParam))
    {
     closesocket(s);
     return 0;

    switch(WSAGETSELECTEVENT(lParam))
    {
    case FD_CONNECT:
    ……;
      return 0;
    case FD_READ:
    ……;
      return 0;
    case FD_WRITE:
    return 0;
    case FD_CLOSE:
    return 0;
    default:
    output=(CEdit*)GetDlgItem(IDC_SHOWTEXT);
    output->SetWindowText("an network error has occured,the connection is dropped");
    closesocket(m_client.m_hSocket);
    m_bInit=false;
    return 0;
    }
    }
      

  3.   

    还有一个问题,我设置的#define SER_MESSAGE WM_USER+102
    afx_msg LRESULT OnServerMessage(WPARAM wParam,LPARAM lParam);//CChatDlg.hBEGIN_MESSAGE_MAP(CChatDlg,CDialog)
    ON_MESSAGE(SER_MESSAGE,OnServerMessage)
    END_MESSAGE_MAP()                        //CChatDlg.cppvoid CServer::ServerInit()
    {
    if(SOCKET_ERROR==WSAAsyncSelect(m_hSocket,m_hWnd,SER_MESSAGE,FD_CLOSE|FD_READ|FD_WRITE|FD_ACCEPT))
    AfxMessageBox("socketerror");
    }LRESULT CChatDlg::OnServerMessage(WPARAM wParam,LPARAM lParam)
    {
    AfxMessageBox("OnServerMessage");
    return 0;
    }在这段代码中ServerInit()函数执行了,并且WSAAsyncSelect()的返回值为0,但是相应的OnServerMesage()函数却没有执行,这是为什么?
    以上相同设置的ClientInit()和OnClientMessage()函数却能执行。再次感谢。