• 大小: 82KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-13
  • 语言: C/C++
  • 标签: Winsock  

资源简介

《Visual C++网络通信编程实用案例精选》配套源码 Winsock实现网络聊天室【\chap1\ChatRoom(Winsock)】 CSocket实现聊天室【\chap1\ChatRoom(Csocket)】

资源截图

代码片段和文件信息

// ClientSocket.cpp : implementation file
//

#include “stdafx.h“
#include “Example2_ChatRoom.h“
#include “ClientSocket.h“
#include “Example2_ChatRoomDlg.h“


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{
m_aSessionIn=NULL;
m_aSessionOut=NULL;
m_sfSocketFile=NULL;
m_bInit=false;
m_bClose=false;

}

CClientSocket::~CClientSocket()
{
if(m_aSessionIn)
delete m_aSessionIn;
if(m_aSessionOut)
delete m_aSessionOut;
if(m_sfSocketFile)
delete m_sfSocketFile;
}


// Do not edit the following lines which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::OnReceive(int nErrorCode) 
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode);
do
{
CMessg temp;
temp.Serialize(*m_aSessionIn);
m_dlg->m_sMsgList+=temp.m_strText;
m_dlg->SetDlgItemText(IDC_SHOWTEXTm_dlg->m_sMsgList);
int linenum=((CEdit *)(m_dlg->GetDlgItem(IDC_SHOWTEXT)))->GetLineCount();
((CEdit *)(m_dlg->GetDlgItem(IDC_SHOWTEXT)))->LineScroll(linenum);
if(!m_dlg->m_bClient)
{
for(POSITION pos=m_dlg->m_connectionList.GetHeadPosition();pos!=NULL;)
{
CClientSocket * t = (CClientSocket*)m_dlg->m_connectionList.GetNext(pos);
if(t->m_hSocket!=this->m_hSocket)
{
t->SendMessage(&temp);
}
}
}
}
while (!m_aSessionIn->IsBufferEmpty());
}

void CClientSocket::Init(CExample2_ChatRoomDlg * dlg)
{
m_sfSocketFile= new CSocketFile(this);
m_aSessionIn=new CArchive(m_sfSocketFileCArchive::load);
m_aSessionOut=new CArchive(m_sfSocketFileCArchive::store);
m_bClose=false;
this->m_dlg=dlg;
}
BOOL CClientSocket::SendMessage(CMessg * msg)
{
if (m_aSessionOut != NULL)
{
msg->Serialize(*m_aSessionOut);
m_aSessionOut->Flush();
return TRUE;
}
else
{
//对方关闭了连接
m_bClose=true;
CloseSocket();
m_dlg->CloseSessionSocket();
return FALSE;
}
}


void CClientSocket::CloseSocket()
{
if(m_aSessionIn)
{
delete m_aSessionIn;
m_aSessionIn=NULL;
}
if(m_aSessionOut)
{
delete m_aSessionOut;
m_aSessionOut=NULL;
}
if(m_sfSocketFile)
{
delete m_aSessionOut;
m_sfSocketFile=NULL;
}
Close();
m_bInit=false;
m_bClose=true;
}

void CClientSocket::OnClose(int nErrorCode) 
{
// TODO: Add your specialized code here and/or call the base class
m_bClose=true;
CloseSocket();
m_dlg->CloseSessionSocket();
CSocket::OnClose(nErrorCode);
}

int CClientSocket::GetLocalHostName(CString &sHostName) //获得本地计算机名称
{
char szHostName[256];
int nRetCode;
nRe

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5949  2003-06-30 15:18  chap1\ChatRoom(Csocket)\ClientSocket.cpp

     文件       1882  2003-06-30 14:39  chap1\ChatRoom(Csocket)\ClientSocket.h

     文件       1104  2003-06-30 14:41  chap1\ChatRoom(Csocket)\CMessg.cpp

     文件        544  2003-06-30 14:39  chap1\ChatRoom(Csocket)\CMessg.h

     文件      21664  2003-12-08 15:42  chap1\ChatRoom(Csocket)\Example2_ChatRoom.aps

     文件       1906  2003-12-08 15:43  chap1\ChatRoom(Csocket)\Example2_ChatRoom.clw

     文件       2217  2003-06-30 10:43  chap1\ChatRoom(Csocket)\Example2_ChatRoom.cpp

     文件       4753  2003-06-30 15:02  chap1\ChatRoom(Csocket)\Example2_ChatRoom.dsp

     文件        559  2003-06-30 10:43  chap1\ChatRoom(Csocket)\Example2_ChatRoom.dsw

     文件       1445  2003-06-30 10:43  chap1\ChatRoom(Csocket)\Example2_ChatRoom.h

     文件     123904  2003-12-08 15:43  chap1\ChatRoom(Csocket)\Example2_ChatRoom.ncb

     文件      56832  2007-08-04 23:26  chap1\ChatRoom(Csocket)\Example2_ChatRoom.opt

     文件       1308  2003-12-08 15:42  chap1\ChatRoom(Csocket)\Example2_ChatRoom.plg

     文件       5963  2003-07-23 16:25  chap1\ChatRoom(Csocket)\Example2_ChatRoom.rc

     文件       8006  2003-07-23 16:28  chap1\ChatRoom(Csocket)\Example2_ChatRoomDlg.cpp

     文件       1908  2003-06-30 14:59  chap1\ChatRoom(Csocket)\Example2_ChatRoomDlg.h

     文件       3777  2003-06-30 10:43  chap1\ChatRoom(Csocket)\ReadMe.txt

     文件       1078  2003-06-30 10:43  chap1\ChatRoom(Csocket)\res\Example2_ChatRoom.ico

     文件        409  2003-06-30 10:43  chap1\ChatRoom(Csocket)\res\Example2_ChatRoom.rc2

     文件        925  2003-07-01 10:39  chap1\ChatRoom(Csocket)\resource.h

     文件       1340  2003-06-30 15:13  chap1\ChatRoom(Csocket)\ServerSocket.cpp

     文件       1346  2003-06-30 14:33  chap1\ChatRoom(Csocket)\ServerSocket.h

     文件        219  2003-06-30 10:43  chap1\ChatRoom(Csocket)\StdAfx.cpp

     文件       1054  2003-06-30 10:43  chap1\ChatRoom(Csocket)\StdAfx.h

     文件       1038  2003-06-27 20:26  chap1\ChatRoom(Winsock)\ChangeNameDlg.cpp

     文件       1253  2003-06-27 20:26  chap1\ChatRoom(Winsock)\ChangeNameDlg.h

     文件       1956  2003-06-27 19:52  chap1\ChatRoom(Winsock)\Client.cpp

     文件        811  2003-06-27 17:12  chap1\ChatRoom(Winsock)\Client.h

     文件      21992  2003-12-08 15:40  chap1\ChatRoom(Winsock)\Example1.aps

     文件       1753  2003-12-08 15:41  chap1\ChatRoom(Winsock)\Example1.clw

............此处省略29个文件信息

评论

共有 条评论