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

📄 chatroom2.cpp

📁 自己做的局域网聊天工具
💻 CPP
字号:
// ChatRoom2.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "ChatRoom2.h"
#include "ChatRoom2Dlg.h"
#include "LoginDlg.h"
#include "ClientSocket.h"


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

/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App

BEGIN_MESSAGE_MAP(CChatRoom2App, CWinApp)
	//{{AFX_MSG_MAP(CChatRoom2App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App construction

CChatRoom2App::CChatRoom2App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CChatRoom2App object

CChatRoom2App theApp;
//发送线程函数
UINT SendDataThread(LPVOID lpParam);
//接收线程函数
UINT RecDataThread(LPVOID lpParam);
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App initialization

BOOL CChatRoom2App::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CChatRoom2Dlg chatdlg;
	m_pMainWnd = &chatdlg;
	if (chatdlg.DoModal() == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
//		//  dismissed with OK
//    	CLoginDlg dlg;
//		int nn;
//		nn = dlg.DoModal();
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
//////////////////////////////////////////////////////////////////////////
//发送数据线程函数
UINT SendDataThread(LPVOID lpParam)
{
	CChatRoom2Dlg *pDlg = (CChatRoom2Dlg *)lpParam;
	CFile file;
	CString str1,str2;
	if (!file.Open(pDlg->m_strFilename,CFile::modeRead))
	{
		AfxMessageBox("打开文件出错");
		return 0;
	}
	pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(FALSE);
	pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(TRUE);

	int iBufSize ;
	iBufSize= 1024*5;
	int iSize = iBufSize;
	LPBYTE pBuf = new BYTE[iBufSize];
	
//	DWORD dwTemp = 0;
//	pDlg->m_sListenSocket.AsyncSelect(0);
// 	pDlg->m_sListenSocket.IOCtl(FIONBIO,&dwTemp);

	UINT ulength = file.GetLength();
	
	pDlg->m_sListenSocket.Send(&ulength,sizeof(UINT));
	
	int iNumByte;
	UINT uTotal = 0;

	while (uTotal<ulength)
	{
		int iEnd;
		iEnd = pDlg->m_bSendEnd;
		iNumByte = pDlg->m_sListenSocket.Send(&iEnd,sizeof(int));
		
		if (iNumByte==SOCKET_ERROR)
		{
			AfxMessageBox("发送出错");
			goto ExitSendData;
		}
		if (iEnd==1)
		{
			AfxMessageBox("发送端中止发送");
			goto ExitSendData;
		}
		//读取文件内容
		if((int)(ulength-uTotal)<iBufSize)
			iSize = ulength-uTotal;
		iSize = file.Read(pBuf,iSize);
		
		int iCount = 0;
		//发送定长数据
		while (iCount<iSize)
		{
			iNumByte = pDlg->m_sListenSocket.Send(pBuf,iSize-iCount);
			if (iNumByte==SOCKET_ERROR)
			{
				AfxMessageBox("发送错误");
				goto ExitSendData;
			}
			iCount += iNumByte;
			if (iCount<iSize)
			{
				file.Seek(iSize-iCount,CFile::current);
			}
		}
		uTotal += iCount;
		//设置发送进度条
		pDlg->m_proSend.SetPos(int(((double)uTotal/ulength)*100));
		str1.Format("发送进度:%d%%",(int)(((double)uTotal/ulength)*100));
		pDlg->GetDlgItemText(IDC_STATIC_SEND,str2);
		if (str1!=str2)
		{
			pDlg->SetDlgItemText(IDC_STATIC_SEND,str1);
		}
		
	}
	AfxMessageBox("文件发送成功");
ExitSendData:
	delete[] pBuf;
	file.Close();
	pDlg->m_sListenSocket.Close();
	pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(FALSE);
	pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(TRUE);
	return 0;


}
//////////////////////////////////////////////////////////////////////////
//接收数据线程函数
UINT RecDataThread(LPVOID lpParam)
{
	CChatRoom2Dlg *pDlg = (CChatRoom2Dlg *)lpParam;
	CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"所有文件 (*.*)*.*");
	
	while (dlg.DoModal()!=IDOK)
	{
		AfxMessageBox("文件选择出错,请重新选择");
	}
	CString str1,str2,str3;
	
	pDlg->GetDlgItem(IDC_RECEND)->EnableWindow(FALSE);
	str3 = dlg.GetPathName();
	CFile file;
	file.Open(str3,CFile::modeCreate|CFile::modeWrite);

	BOOL bFileFail = FALSE;
//	DWORD dwTemp = 0;
//	pDlg->m_cListenSocket.AsyncSelect(0);
//	pDlg->m_cListenSocket.IOCtl(FIONBIO,&dwTemp);

	UINT ulength;
	pDlg->m_cListenSocket.Receive(&ulength,sizeof(UINT));
	
	int iBufSize = 1024*5;
	int iSize = iBufSize;
	LPBYTE pBuf = new BYTE[iBufSize];

	int iNumByte;
	UINT uTotal = 0; 
	while (uTotal<ulength)
	{
		int iEnd = 0;
		if (pDlg->m_bRecEnd)
		{
			AfxMessageBox("接收端中止");
			goto ExitRecData;
		}
		iNumByte = pDlg->m_cListenSocket.Receive(&iEnd,sizeof(int));
		if (iEnd==SOCKET_ERROR)
		{
			AfxMessageBox("接收数据出错");
			goto ExitRecData;
		}
		if (iEnd==1)
		{
			AfxMessageBox("发送端中止");
			goto ExitRecData;
		}
		if ((int)(ulength-uTotal)<iBufSize)
		{
			iSize = ulength-uTotal;
		}
		int iCount = 0;
		while (iCount<iSize)
		{
			iNumByte = pDlg->m_cListenSocket.Receive(pBuf,iSize-iCount);
			if (iNumByte==SOCKET_ERROR)
			{
				AfxMessageBox("接收数据出错");
				goto ExitRecData;
			}
			iCount += iNumByte;
			file.Write(pBuf,iNumByte);
			
		}
		uTotal += iCount;
		pDlg->m_proRec.SetPos(int(((double)uTotal/ulength)*100));
		pDlg->GetDlgItem(IDC_STATIC_REC)->GetWindowText(str2);
		str1.Format("接收进度:%d%%",int(((double)uTotal/ulength)*100));
		if(str1!=str2)
			pDlg->SetDlgItemText(IDC_STATIC_REC,str1);
	}
	AfxMessageBox("接收数据成功");
	bFileFail = FALSE;
ExitRecData:
	delete[] pBuf;
	file.Close();
	if (!bFileFail)
	{
		CFile::Remove(str3);
	}
	pDlg->m_cListenSocket.Close();
	pDlg->m_proRec.SetPos(0);
	pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(TRUE);
	pDlg->GetDlgItem(IDC_RECEND)->EnableWindow(TRUE);
	pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(TRUE);
	pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText("接收数据:");
	return 0;
	
}

⌨️ 快捷键说明

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