gusucode.com > 基于VC++的局域网视频聊天系统源码程序 > 基于VC++的局域网视频聊天系统源码程序/code/ChatClient/SocketClient.cpp
// SocketClient.cpp : implementation file // #include "stdafx.h" #include "ChatClient.h" #include "SocketClient.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "Mesg.h" #include "ChatClientDlg.h" ///////////////////////////////////////////////////////////////////////////// // CSocketClient CSocketClient::CSocketClient() { m_sfSocketFile = NULL; m_aSessionIn = NULL ; m_aSessionOut = NULL ; } CSocketClient::~CSocketClient() { if ( m_aSessionIn ) { delete m_aSessionIn ; m_aSessionIn = NULL; } if ( m_aSessionOut ) { delete m_aSessionOut ; m_aSessionOut = NULL; } if ( m_sfSocketFile ) { delete m_sfSocketFile ; m_aSessionOut = NULL; } } // Do not edit the following lines, which are needed by ClassWizard. #if 0 BEGIN_MESSAGE_MAP(CSocketClient, CSocket) //{{AFX_MSG_MAP(CSocketClient) //}}AFX_MSG_MAP END_MESSAGE_MAP() #endif // 0 ///////////////////////////////////////////////////////////////////////////// // CSocketClient member functions //发送消息 void CSocketClient::SendM(CMesg *msg) { if( m_aSessionOut != NULL) { msg->Serialize(* m_aSessionOut); m_aSessionOut->Flush(); } } void CSocketClient::Init(CChatClientDlg *Dlg) { m_sfSocketFile = new CSocketFile(this); m_aSessionIn = new CArchive(m_sfSocketFile,CArchive::load,4096); m_aSessionOut = new CArchive(m_sfSocketFile,CArchive::store,4096); m_dlgMain = Dlg; } //接收信息 void CSocketClient::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class do { CMesg temp; temp.Serialize( *m_aSessionIn ); GetMessgsInfo(&temp) ; } while( !m_aSessionIn->IsBufferEmpty() ); CSocket::OnReceive(nErrorCode); } BOOL CSocketClient::GetMessgsInfo(CMesg *msg) { //用户聊天信息的处理 if(msg->m_szCommand.Compare("Message") == 0 ) { if(msg->m_szRecObject.Compare(m_dlgMain->m_szID)== 0) { m_dlgMain->SetMessageBox(msg->m_szText); return TRUE; } } //更新用户IP和ID信息 if(msg->m_szCommand.Compare("UpdateUser") == 0 ) { int Spos=0,Epos=0; CString UserInfo; CString tempID,tempIP; if(msg->m_szRecObject.Compare(m_dlgMain->m_szID)== 0) { UserInfo = msg->m_szText; for (int i = 1 ;i <= msg->m_iOnlineNum ; i++) { //ID Epos = UserInfo.Find(" ",Spos); tempID = UserInfo.Mid(Spos,Epos - Spos); Spos = Epos+1; //IP Epos = UserInfo.Find(" ",Spos); tempIP = UserInfo.Mid(Spos,Epos - Spos); Spos = Epos+1; m_dlgMain->AddUserToList(tempID); m_dlgMain->AddUserInfoToChain(tempID,tempIP); } m_dlgMain->SetOnlineNum(msg->m_iOnlineNum); return TRUE; } } //系统消息 if(msg->m_szCommand.Compare("SystemMeg") == 0 ) { if(msg->m_szRecObject.Compare(m_dlgMain->m_szID)== 0) { m_dlgMain->SetOnlineNum(msg->m_iOnlineNum); m_dlgMain->DelUserFromList(msg->m_szID); m_dlgMain->DelUserFromList(msg->m_szID); m_dlgMain->SetMessageBox(msg->m_szText); return TRUE; } } //新用户加入 if(msg->m_szCommand.Compare("NewUser") == 0 ) { if(msg->m_szRecObject.Compare(m_dlgMain->m_szID)== 0) { m_dlgMain->SetOnlineNum(msg->m_iOnlineNum); m_dlgMain->AddUserToList(msg->m_szID); m_dlgMain->AddUserInfoToChain( msg->m_szID,msg->m_szIP); m_dlgMain->SetMessageBox(msg->m_szText); return TRUE; } } //登陆验证 if(msg->m_szCommand.Compare("RLogin") == 0 ) { if(msg->m_szRecObject.Compare(m_dlgMain->m_szID)== 0) { if(msg->m_szText.Compare("验证通过") == 0) //验证通过 { m_dlgMain->EnableWindow(TRUE); return TRUE; } else { MessageBox(NULL,msg->m_szText,"错误",MB_OK); if(m_dlgMain->LoginDlgCtrl() == -1) m_dlgMain->EndDialog(1); return FALSE; } } } return TRUE; } void CSocketClient::OnClose(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class m_dlgMain->SetMessageBox("与服务器连接中断或者服务器已经关闭!\r\n"); //其它设置 m_dlgMain->m_bConnect = false; m_dlgMain->SetOnlineNum(0); m_dlgMain->SetListEmpty(); m_dlgMain->SetUserNameEmpty(); delete this; CSocket::OnClose(nErrorCode); }