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

📄 ctssocket.cpp

📁 基于UDP协议的winsock聊天室
💻 CPP
字号:
// CTSSocket.cpp : implementation file
//

#include "stdafx.h"
#include "client.h"
#include "CTSSocket.h"
#include "ClientDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCTSSocket

CCTSSocket::CCTSSocket()
{
}

CCTSSocket::~CCTSSocket()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CCTSSocket member functions

void CCTSSocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CClientDlg *pClientDlg = (CClientDlg *)AfxGetMainWnd();
	CString strMessage, strRichEdit;
	CString strAddress, strNickName, strPort;
	UINT nPort;
	int i,j;
	char szMessage[128];
	CUser *pUser = NULL;
	memset(szMessage, 0, 128);
	Receive(szMessage, 128);
//	pClientDlg->MessageBox(szMessage);
	strMessage.Format("%s", szMessage);
	i = strMessage.Find('/');
	strAddress = strMessage.Mid(3, i-3);
	j = strMessage.Find('/', i+1);
	strPort = strMessage.Mid(i+1, j-i-1);
	nPort = atoi(strPort);
	strNickName = strMessage.Mid(j+1, strMessage.GetLength()-j-1);
	CListBox *pListBox = (CListBox *)pClientDlg->GetDlgItem(IDC_MEMBERLIST);
	pClientDlg->GetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
	
	if(strMessage.Mid(0,3).Compare("Add") == 0)
	{
//		pClientDlg->MessageBox(strNickName);
		if(pListBox->FindString(0, strNickName) == LB_ERR)
		{
			pUser = new CUser(strAddress, nPort, strNickName);			
			pClientDlg->m_UserList.AddTail(pUser);
			pListBox->AddString(strNickName);
			pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
		}
	}
	else
		if(strMessage.Mid(0,3).Compare("Del") == 0)
		{
			if(strNickName == pClientDlg->m_strNickname)	//自己被踢出
			{
				strRichEdit += "\r\n***** 您被踢出了聊天室 *****";
				pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
				CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
				if(pRich->GetLineCount() > 14)
				{
					pRich->LineScroll(1, 0);
					pRich->LineScroll(1, 0);
				}
				pListBox->ResetContent();
				pClientDlg->SetDlgItemText(IDC_MEMBERSELECT, "");
				pClientDlg->SetDlgItemText(IDC_MESSAGEINPUT, "");
				pClientDlg->SetDlgItemText(IDC_EDIT_Memcount, "");
			}
			else				//其他人正常退出
			{
				for(i=0; i<pClientDlg->m_UserList.GetCount(); i++)
				{
					pUser = pClientDlg->m_UserList.GetAt(pClientDlg->m_UserList.FindIndex(i));
					if(pUser->m_strNickName = strNickName)
					{
						pClientDlg->m_UserList.RemoveAt(pClientDlg->m_UserList.FindIndex(i));
						pListBox->DeleteString(pListBox->FindString(0, strNickName));
						strRichEdit += "\r\n***** " + strNickName + " 离开了聊天室 *****";
						pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
						break;
					}
					CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
		
					if(pRich->GetLineCount() > 14)
					{
						pRich->LineScroll(1, 0);
						pRich->LineScroll(1, 0);
					}
				}
			}
		}
		long MCount=pClientDlg->m_UserList.GetCount();
		CString temp;temp.Format("%d",MCount);
		pClientDlg->SetDlgItemText(IDC_EDIT_Memcount,temp);

	CSocket::OnReceive(nErrorCode);
}

⌨️ 快捷键说明

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