我做了一个c/s程序,支持多个客户端。server端发送消息,为何同时连接的多个客户端中只有最近连接的一个能收到?但多个客户端给服务器发的消息,服务器都能正常接受。
欢迎各位大侠指教,谢谢了!

解决方案 »

  1.   

    server - clienta 之间有个socketa
    server - clientb 之间有个socketb
    ...你是不是服务端发消息了只用socketa发消息了啊
    而没有用socketb, socketc,发消息啊
      

  2.   

    搞点code出来,也许是你编码错误
      

  3.   

    代码如下:Server端发送
    void CListeningSocket::OnAccept(int nErrorCode) 
    {
      // TODO: Add your specialized code here and/or call the base class
      CClientSocket* pSocket=new CClientSocket(m_pMyServerDlg);
      if(m_pMyServerDlg->m_pListeningSocket->Accept(*pSocket))
    {
    m_pMyServerDlg->m_pConnectionList.AddTail(pSocket);
    m_pMyServerDlg->m_pClientSocket=pSocket;
    m_pMyServerDlg->m_list.InsertString(0,"有客户端连接!");
    }
    else
    delete pSocket;
    }void CMyServerDlg::OnSend() 
    {
      // TODO: Add your control notification handler code here
        if(m_pClientSocket)
    {
    char pMsg[10000];
    GetDlgItem(IDC_SENDDATA_EDIT)->GetWindowText(pMsg,10000);
    SendMsg(m_pClientSocket,pMsg);
    }
    return;
    }void CMyServerDlg::SendMsg(CClientSocket* pClientSocket,char* pMsg)
    {

    pClientSocket->Send(pMsg,strlen(pMsg));
    }Client端接收:void CMyClientDlg::FetchMsg(CRequestSocket *pRequestSocket)
    {
    char pMsg[1000],tempMsg[1000];
    int ByteCount;
    int EndFlag=0;
    CStringArray* temp = new CStringArray; strcpy(pMsg,"");
    do
    {
    strcpy(tempMsg,"");
    ByteCount=pRequestSocket->Receive(tempMsg,1000);
    if(ByteCount>1000 || ByteCount<=0)
    {
    m_list.InsertString(0,"接受信息错误");
    return;
    }
    else if (ByteCount<1000 && ByteCount>0)
    {
    EndFlag=1;
    }
                      tempMsg[ByteCount]=0;
    strcat(pMsg,tempMsg);
    }while(EndFlag==0);
    }
    急呀!!!!!!!谢谢各位了!!!!!!!!!
      

  4.   

    从代码看,server发送数据后,只能是最后一个连接的client才能收到信息,因为m_pMyServerDlg->m_pClientSocket=pSocket;而且SendMsg(m_pClientSocket,pMsg);我想你用循环发送就可以了(从m_pConnectionList取出每一个client socket)