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

📄 clientsocket.cpp

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

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

#ifdef _DEBUG
#define new DEBUG_NEW
#define clGreen RGB(0,255,0)
#define clRed RGB(255,0,0)
#define clBlue RGB(0,0,255)
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{
}

CClientSocket::~CClientSocket()
{
}


// 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
	CClientDlg *pClientDlg = (CClientDlg *)AfxGetMainWnd();
	CString PeerAddress, str, Tempstr, strFileTitle, strFileLen, strShowMsg, MsgTo;
	long nFilelen=0;
	UINT nPeerPort;
	int i=0, j=0;
	CUser *pUser;
	char szMessage[8192];
	memset(szMessage, 0, 8192);
	ReceiveFrom(szMessage, 8192, PeerAddress, nPeerPort);
	Tempstr = szMessage;
	if(Tempstr.Mid(0, 9).Compare("SendFile/") == 0)
	{
		for(i=0; i<pClientDlg->m_UserList.GetCount(); i++)
		{
			pUser = pClientDlg->m_UserList.GetAt(pClientDlg->m_UserList.FindIndex(i));
			if(pUser->m_strAddress == PeerAddress && pUser->m_nPort == nPeerPort)
				break;
		}
		i = Tempstr.Find("/", 9);//从第9个byte开始找到下一个/
		j = Tempstr.Find("/", i+1);//从第i+1个byte开始找到下一个/
		nPeerPort = atoi(Tempstr.Mid(9,i-9));
		strFileTitle = Tempstr.Mid(i+1, j-i-1);
		pClientDlg->m_strFileTitle = strFileTitle;
		strFileLen = Tempstr.Mid(j+1);
		pClientDlg->m_nFilelen = atol(strFileLen);
		strShowMsg = pUser->m_strNickName + " 要向您发送\r\n" + strFileTitle + "(" + strFileLen + " Bytes)\r\n是否接收?";
		if(pClientDlg->MessageBox(strShowMsg, "接收文件", MB_OKCANCEL|MB_ICONQUESTION) == IDOK)
		{
			delete pClientDlg->m_SaveDlg;
			pClientDlg->m_SaveDlg = new CFileDialog(false);
			if(pClientDlg->m_SaveDlg->DoModal() == IDOK)
			{
				pClientDlg->m_strSavePath = pClientDlg->m_SaveDlg->m_ofn.lpstrFile;//有点问题
				pClientDlg->m_FileSocket.Create();
				pClientDlg->m_FileSocket.Connect(PeerAddress, nPeerPort);
			}
			else
			{
				SendTo("RefuseRcv", 9, pUser->m_nPort, pUser->m_strAddress);
			}
		}
		else
		{
			SendTo("RefuseRcv", 9, pUser->m_nPort, pUser->m_strAddress);
		}
	}
	else
	{
		if(Tempstr.Mid(0, 9).Compare("RefuseRcv") == 0)
		{
			pClientDlg->m_FileSocket.Close();
			pClientDlg->GetDlgItemText(IDC_MEMBERSELECT, MsgTo);
			pClientDlg->GetDlgItemText(IDC_MESSAGEDISP, str);
			str += "\r\n" + MsgTo + " 拒绝接收文件";
			pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, str);

			CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
	
			if(pRich->GetLineCount() > 14)
			{
				pRich->LineScroll(1, 0);
				pRich->LineScroll(1, 0);
			}
		}
		else
		{
			pClientDlg->GetDlgItemText(IDC_MESSAGEDISP, str);
			str += "\r\n";
			str += szMessage;
/*			CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
			SetColour(pRich,str,clBlue);*/
			pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, str);
		}
	}
	CSocket::OnReceive(nErrorCode);
}

void CClientSocket::SetColour(CRichEditCtrl *rich, LPCTSTR lpszMessage, COLORREF clr)
{
	CHARFORMAT cf;
	ZeroMemory(&cf, sizeof(CHARFORMAT));
	cf.cbSize=sizeof(CHARFORMAT);   
	cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE;   
	if(cf.dwEffects&CFE_AUTOCOLOR)cf.dwEffects-=CFE_AUTOCOLOR;   
	 //Get a color from the common color dialog.   
	cf.crTextColor=clr;   
	cf.dwMask=CFM_COLOR;   
	rich->SetSelectionCharFormat(cf);   
	rich->ReplaceSel(lpszMessage);   
}

⌨️ 快捷键说明

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