服务段端发送函数:void CBITDlg::SendMsg()

if(!m_SocketList.IsEmpty())
{
UpdateData(true);
POSITION pos;
CClientSocket* pSocket=(CClientSocket*)m_SocketList.GetHead();
int count;
for(pos=m_SocketList.GetHeadPosition();pos!=NULL;)
{
pSocket=(CClientSocket*)m_SocketList.GetNext(pos);

pSocket->Send(sen,sen.GetLength()); }
count=sen.GetLength();
sen.Empty();
UpdateData(false);

}
else 
    MessageBox(_T("未建立有Socket连接,无法传递消息!"),_T("服务器程序"));



}客户端接收函数:
void CBITRevDlg::ReceiveMsg(CChatSocket *pSocket)
{wchar_t buff[1000];
int count;
count=pSocket->Receive(buff,1000);
buff[count]=0;b.Format(_T("%s"),buff); 
UpdateData(false);
}当从服务器端向客户端发送数据时会发生数据丢失
如:(服务器端) 456789123->(客户端)4567참쳌쳌쳌쳌

解决方案 »

  1.   

    for(pos=m_SocketList.GetHeadPosition();pos!=NULL;)
    ????
      

  2.   

    这个应该没问题   那是有的时候可能建多对socket
      

  3.   

    把创建socket的代码贴出来看看,可能那出了问题
      

  4.   

    class CClientSocket : public CSocket
    {
    DECLARE_DYNAMIC(CClientSocket);
    private:
    CClientSocket(const CClientSocket& rSrc);         // no implementation
    void operator=(const CClientSocket& rSrc);  // no implementation// Construction
    public:
    CClientSocket();// Attributes
    public:
    // Operations // Overridable callbacks
    protected:
    virtual void OnReceive(int nErrorCode);// Implementation
    public:
    virtual ~CClientSocket();#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif
    };
    void CClientSocket::OnReceive(int nErrorCode)
    {

    ((CBITDlg*)(AfxGetApp()->m_pMainWnd))->ReceiveMsg(this);
    CSocket::OnReceive(nErrorCode);
    }class CListeningSocket : public CSocket
    {
    DECLARE_DYNAMIC(CListeningSocket);
    private:
    CListeningSocket(const CListeningSocket& rSrc);         // no implementation
    void operator=(const CListeningSocket& rSrc);  // no implementation// Construction
    public:
    CListeningSocket();// Attributes
    public:
    // Overridable callbacks
    protected:
    virtual void OnAccept(int nErrorCode);// Implementation
    public:
    virtual ~CListeningSocket();#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif
    };void CListeningSocket::OnAccept(int nErrorCode)
    {  CClientSocket* pSocket=new CClientSocket();
       CBITDlg* pMainWnd=
       ((CBITDlg*)(AfxGetApp()->m_pMainWnd));
       if(pMainWnd->m_pSocket->Accept(*pSocket))
       pMainWnd->m_SocketList.AddTail(pSocket);
       else 
       delete pSocket;
    CSocket::OnAccept(nErrorCode);

    }
      

  5.   

    class CChatSocket : public CSocket
    {
    DECLARE_DYNAMIC(CChatSocket);// Construction
    public:
    CChatSocket();// Operations
    public:
    // Implementation
    protected:
    virtual void OnReceive(int nErrorCode);
    };
    void CChatSocket::OnReceive(int nErrorCode){

    ((CBITRevDlg*)(AfxGetApp()->m_pMainWnd))->ReceiveMsg(this);
    CSocket::OnReceive(nErrorCode);
    }
      

  6.   

    问题解决了
    不过还是挺模糊的pSocket->Send(LPCTSTR(sen),sen.GetLength());--->pSocket->Send(LPCTSTR(sen),1000);