• 大小: 613KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: 其他
  • 标签: 聊天室  

资源简介

一个简单的聊天室程序源代码!基于Winsock,很好的基础的VC网络编写代码!

资源截图

代码片段和文件信息

// 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);

if(temp.m_tag==1&&m_dlg->m_willchating==FALSE)
//如果有声音过来并且本机的声音设备已经准备好了则首先在本机发出声音
{
memcpy(m_dlg->m_sound.m_cBufferOuttemp.m_bufferMAX_BUFFER_SIZE);
}
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->Clos

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

     文件       6154  2003-07-15 14:45  ChatRoom\ClientSocket.cpp

     文件       1882  2003-06-30 14:39  ChatRoom\ClientSocket.h

     文件       1401  2003-07-15 14:37  ChatRoom\CMessg.cpp

     文件        657  2003-07-15 14:33  ChatRoom\CMessg.h

     文件          0  2003-07-15 10:01  ChatRoom\data.txt

     文件       1999  2003-10-21 21:02  ChatRoom\Example2_ChatRoom.clw

     文件       2217  2003-06-30 10:43  ChatRoom\Example2_ChatRoom.cpp

     文件       4928  2003-08-14 11:09  ChatRoom\Example2_ChatRoom.dsp

     文件        559  2003-06-30 10:43  ChatRoom\Example2_ChatRoom.dsw

     文件       1445  2003-06-30 10:43  ChatRoom\Example2_ChatRoom.h

     文件       6132  2003-07-15 14:48  ChatRoom\Example2_ChatRoom.rc

     文件      10865  2003-07-15 18:47  ChatRoom\Example2_ChatRoomDlg.cpp

     文件       2194  2003-07-15 10:38  ChatRoom\Example2_ChatRoomDlg.h

     文件     575488  2003-07-15 14:39  ChatRoom\newdata.txt

     文件       3777  2003-06-30 10:43  ChatRoom\ReadMe.txt

    .......      1078  2003-06-30 10:43  ChatRoom\res\Example2_ChatRoom.ico

    .......       409  2003-06-30 10:43  ChatRoom\res\Example2_ChatRoom.rc2

     文件       1017  2003-07-14 19:43  ChatRoom\resource.h

     文件       1340  2003-06-30 15:13  ChatRoom\ServerSocket.cpp

     文件       1346  2003-06-30 14:33  ChatRoom\ServerSocket.h

    .......    493056  2003-02-13 16:39  ChatRoom\Sound\Sound.dll

    .......      2500  2003-02-13 16:39  ChatRoom\Sound\Sound.lib

    .......       467  2003-02-13 16:33  ChatRoom\Sound\USound2.h

     文件       4990  2003-10-21 21:01  ChatRoom\Sound.cpp

     文件       1071  2003-08-14 11:09  ChatRoom\Sound.h

     文件        219  2003-06-30 10:43  ChatRoom\StdAfx.cpp

     文件       1054  2003-06-30 10:43  ChatRoom\StdAfx.h

     目录          0  2008-07-16 21:33  ChatRoom\res

     目录          0  2008-07-16 21:33  ChatRoom\Sound

     目录          0  2008-07-16 21:33  ChatRoom

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

评论

共有 条评论