⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainsocket.cpp

📁 实现了网络通信.具有类似QQ的功能,具有客户端与服务器
💻 CPP
字号:
// MainSocket.cpp : implementation file
//

#include "stdafx.h"
#include "MyQQClient.h"
#include "MainSocket.h"
#include "MyQQClientDlg.h"
#include "ChatDialog.h"
#include "LoginLoad.h"
#include "ChatAction.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainSocket

CMainSocket::CMainSocket()
{
}

CMainSocket::~CMainSocket()
{
	CHATDLGINFO *pGlgInfo;
	for (int i=0;i<m_ChatInfo.GetSize();i++)
	{
		pGlgInfo = (CHATDLGINFO *)m_ChatInfo.GetAt(i);
		delete pGlgInfo;
	}
	m_ChatInfo.RemoveAll();
	CChatDialog *pDlgChat;
	while(m_ChatDlgs.GetSize() > 0)
	{
		pDlgChat = (CChatDialog *)m_ChatDlgs.GetAt(0);
		m_ChatDlgs.RemoveAt(0);
		pDlgChat->DestroyWindow();
	}
	CChatAction * pChatAction;
	while(m_ChatActions.GetSize() > 0)
	{
		pChatAction = (CChatAction *)m_ChatActions.GetAt(i);
		m_ChatActions.RemoveAt(0);
		pChatAction->DestroyWindow();
	}

	//m_ChatDlgs.RemoveAll();
}


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

/////////////////////////////////////////////////////////////////////////////
// CMainSocket member functions

void CMainSocket::OnReceive(int nErrorCode) 
{
	CMyQQClientDlg * pWnd = static_cast <CMyQQClientDlg *>(AfxGetMainWnd());
	Message msContent;
	int iLen1 = sizeof(msContent);	int iLen2 = sizeof(Message);
	memset(&msContent,0,sizeof(msContent));
	int iLen = Receive(&msContent,sizeof(msContent));

	pWnd->SendMessage(WM_RECEIVEMESSAGE,WPARAM(&msContent),0);
	CSocket::OnReceive(nErrorCode);
}

void CMainSocket::SetUserName(CString sName)
{
	m_UserName = sName;
}

CString CMainSocket::GetUserName()
{
	return m_UserName;
}

void CMainSocket::SetImage(int image)
{
	m_image = image;
}

int CMainSocket::GetImage()
{
	return m_image;
}

void CMainSocket::OpenChatDlg(CString UserName)
{
	CChatDialog *pDlgChat;
	for(int i=0;i<m_ChatDlgs.GetSize();i++)
	{
		pDlgChat = (CChatDialog *)m_ChatDlgs.GetAt(i);
		if (pDlgChat->m_ClientName == UserName)
		{
			if (pDlgChat->IsIconic())
				pDlgChat->SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
			pDlgChat->SetForegroundWindow();
			return;
		}
	}
	CHATDLGINFO *pInfo;
	for (i=0;i<m_ChatInfo.GetSize();i++)
	{
		pInfo = (CHATDLGINFO *)m_ChatInfo.GetAt(i);
		if (pInfo->strUserName == UserName)
		{
			pDlgChat = new CChatDialog();
			pDlgChat->m_pClientSocket = this;
			pDlgChat->m_UserName = UserName;
			CWnd *pWnd = ((CMyQQClientDlg *)m_pMainDlg)->GetDesktopWindow();
			pDlgChat->Create(IDD_DIALOG_CHAT,pWnd);
			
			int nFrom = 0;
			int nPos = 0;
			CString strChat;
			CString strName;
			while(nFrom < pInfo->strChatMessage.GetLength())
			{
				nPos = pInfo->strChatMessage.Find("\r\n",nFrom);
				if (nPos < 0) break;
				strName = pInfo->strChatMessage.Mid(nFrom,nPos - nFrom - 5);
				nFrom = nPos + 6;
				nPos = pInfo->strChatMessage.Find("\r\n",nFrom);
				if (nPos < 0) break;
				strChat = pInfo->strChatMessage.Mid(nFrom,nPos - nFrom);
				nFrom = nPos + 2;
				pDlgChat->DisplayMessage(strName,strChat);
			}
			m_ChatDlgs.Add((CObject *)pDlgChat);
			delete pInfo;
			m_ChatInfo.RemoveAt(i);
			pDlgChat->ShowWindow(SW_SHOW);
			return;
		}
	}
	pDlgChat = new CChatDialog();

	CWnd *pWnd = ((CMyQQClientDlg *)m_pMainDlg)->GetDesktopWindow();
	pDlgChat->Create(IDD_DIALOG_CHAT,pWnd);
	
	pDlgChat->m_pClientSocket = this;
	pDlgChat->m_ClientName = UserName;
	pDlgChat->m_UserName = m_UserName;

	m_ChatDlgs.Add((CObject *)pDlgChat);

	pDlgChat->ShowWindow(SW_SHOW);	
}

void CMainSocket::SendMessage(CString UserName, CString strMessage)
{

	Message msObj;
	msObj.iType = USERSESSION;
	msObj.iSubType = SAYINPRIVATE;
	CString strTemp = UserName;
	int iLen = strTemp.GetLength();
	iLen > 20 ? 20 : iLen;
	lstrcpy(msObj.strClientName,strTemp.GetBuffer(iLen));

	strTemp = m_UserName;
	iLen = strTemp.GetLength();
	iLen > 20 ? 20 : iLen;
	lstrcpy(msObj.strName,strTemp.GetBuffer(iLen));

	strTemp = strMessage;
	iLen = strTemp.GetLength();
	iLen > 1024 ? 1024 : iLen;
	lstrcpy(msObj.strContent,strTemp.GetBuffer(iLen));
	
	msObj.iImage = m_image;
	Send(&msObj,sizeof(msObj));

}

void CMainSocket::ReceiveMessage(CString UserName, CString strMessage)
{
	CChatDialog *pDlgChat;
	for (int i=0;i<m_ChatDlgs.GetSize();i++)
	{
		pDlgChat = (CChatDialog *)m_ChatDlgs.GetAt(i);
		if (pDlgChat->m_UserName == UserName)
		{
			pDlgChat->DisplayMessage(UserName,strMessage);
			pDlgChat->FlashWindow(TRUE);
			return;
		}
	}
	CHATDLGINFO *pInfo;
	for (i=0;i<m_ChatInfo.GetSize();i++)
	{
		pInfo = (CHATDLGINFO *)m_ChatInfo.GetAt(i);
		if (pInfo->strUserName == UserName)
		{
			pInfo->strChatMessage += UserName + " 说:\r\n" +"    " + strMessage + "\r\n";
			return;
		}
	}
	pInfo = new CHATDLGINFO;
	pInfo->strUserName = UserName;

	pInfo->strChatMessage = UserName + " 说:\r\n" +"    " + strMessage + "\r\n";
	m_ChatInfo.Add((CObject *)pInfo);
}
/*
void CMainSocket::CloseChatDlg(CChatDialog *ChatDlg)
{
	CChatDialog *pChatDlg;
	int nSize = m_ChatDlgs.GetSize();
	for (int i=0;i<m_ChatDlgs.GetSize();i++)
	{
		pChatDlg = (CChatDialog *)m_ChatDlgs.GetAt(i);
		if (pChatDlg == ChatDlg)
		{
			m_ChatDlgs.RemoveAt(i);
			delete pChatDlg;
			break;
		}
	}
}
*/
void CMainSocket::OnChatAction(CString strName,CString strClientName,CString strMessage)
{
	
	for(int i=0; i<m_ChatDlgs.GetSize();i++)
	{
		CChatDialog * ChatDlg = (CChatDialog *)m_ChatDlgs.GetAt(i);
		if(ChatDlg->m_ClientName == strName)
		{
			ChatDlg->DisplayMessage(strName,strMessage);
			return;
			break;
			
		}
	}
	CChatAction * ActionDlg;
	ActionDlg = new CChatAction();
	ActionDlg->m_strClientName = strClientName;
	ActionDlg->m_strName = strName;
	ActionDlg->m_strConnect = strMessage;
	ActionDlg->m_pMainSocket = this;
	ActionDlg->Create(IDD_CHAT_ACTION);
	ActionDlg->ShowWindow(SW_SHOW);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -