-
大小: 17KB文件类型: .zip金币: 2下载: 2 次发布日期: 2021-05-25
- 语言: C/C++
- 标签:
资源简介
MFC基于TCP/IP协议的聊天室源代码 (继承CSocket)
集客户端和服务器与一体;用户界面友好;
代码片段和文件信息
// ClientSocket.cpp : implementation file
//
#include “stdafx.h“
#include “TcpChatRoom.h“
#include “ClientSocket.h“
#include “TcpChatRoomDlg.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(CTcpChatRoomDlg * 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;
nRetCode=gethostname(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-02-26 22:14 TcpChatRoom\
文件 1104 2012-02-26 22:13 TcpChatRoom\CMessg.cpp
文件 544 2012-02-26 22:13 TcpChatRoom\CMessg.h
文件 5931 2012-02-26 22:13 TcpChatRoom\ClientSocket.cpp
文件 1858 2012-02-26 22:13 TcpChatRoom\ClientSocket.h
文件 1322 2012-02-26 22:13 TcpChatRoom\ServerSocket.cpp
文件 1328 2012-02-26 22:13 TcpChatRoom\ServerSocket.h
文件 213 2012-02-26 22:13 TcpChatRoom\StdAfx.cpp
文件 1054 2012-02-26 22:13 TcpChatRoom\StdAfx.h
文件 2133 2012-02-26 22:13 TcpChatRoom\TcpChatRoom.cpp
文件 4645 2012-02-26 22:13 TcpChatRoom\TcpChatRoom.dsp
文件 547 2012-02-26 22:13 TcpChatRoom\TcpChatRoom.dsw
文件 1379 2012-02-26 22:13 TcpChatRoom\TcpChatRoom.h
文件 5884 2012-02-26 22:13 TcpChatRoom\TcpChatRoom.rc
文件 7862 2012-02-26 22:13 TcpChatRoom\TcpChatRoomDlg.cpp
文件 1842 2012-02-26 22:13 TcpChatRoom\TcpChatRoomDlg.h
目录 0 2012-02-26 22:14 TcpChatRoom\res\
文件 1078 2003-06-30 10:43 TcpChatRoom\res\TcpChatRoom.ico
文件 403 2012-02-26 22:13 TcpChatRoom\res\TcpChatRoom.rc2
文件 913 2012-02-26 22:13 TcpChatRoom\resource.h
- 上一篇:c++小球反弹运动
- 下一篇:VC++编写的音频播放器代码
评论
共有 条评论