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

📄 clientsocket.cpp

📁 这是一个用MFC实现CSocket程序,能够一定的聊天功能,是基于网络局域网的一个通讯工具.
💻 CPP
字号:
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Sever.h"
#include "ClientSocket.h"
#include "SeverDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#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
	char flag[2]={0};
	if(Receive(flag,2)!=2)
		return;

	if(flag[0]=='F')
	{

	FILEINFO myFileInfo;
	Receive(&myFileInfo,sizeof(myFileInfo));
	
	CFileDialog dlg(FALSE,"123","456",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
		"所有文件 (*.*)|*.*\0");

	if(dlg.DoModal()!=IDOK)
	{
		return ;
	}

	Send("Y",2);
	Sleep(300);
	

	CString str;
	str.Format(myFileInfo.fileName);

	CFile file;
	file.Open(str,CFile::modeCreate|CFile::modeWrite);
	
	UINT uiLength;
	uiLength=(UINT)myFileInfo.fileLength;
	
	int iBufSize=1024*5;
	int iSize=iBufSize;
	LPBYTE pBuf=new BYTE[iBufSize];
	int iNumByte;
	UINT uiTotal=0;


	while(uiTotal<uiLength)
	{
		if((int)(uiLength-uiTotal)<iBufSize)
			iSize=uiLength-uiTotal;
		int iCount=0;
		while(iCount<iSize)
		{
			iNumByte=Receive(pBuf,iSize-iCount);
			if(iNumByte==SOCKET_ERROR)
			{
				AfxMessageBox("接收错误!");
			}
			iCount+=iNumByte;
			file.Write(pBuf,iNumByte);
		}
		uiTotal+=iCount;
		CSeverApp* pApp=(CSeverApp*)AfxGetApp();
		CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;
		
		CString str;
		str.Format( "%d%%",int(((double)uiTotal/uiLength)*100) );
		pDlg->GetDlgItem(IDC_STATIC_RECEIVE)->SetWindowText(str);
	}
	AfxMessageBox("接收文件成功!");

	}
	else if(flag[0]=='M')
	{
		
	char buffer[1024]={0};
	int len=Receive(buffer,1024);
	if(!len)
	{
		AfxMessageBox("接收信息异常!");
		return;
	}
	CSeverApp* pApp=(CSeverApp*)AfxGetApp();  
	CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;  
	pDlg->m_MSGG.InsertString(0,buffer);             

	POSITION pos=pDlg->m_list.GetHeadPosition();     //在CClientSocket类中,可通过主对话框指针方便的访问定义在其中的套接字容器类!
	while(pos!=NULL)
	{
		if(pos==pDlg->m_list.Find(this))     //  m_list.Find(this)为找到当前用来与客户端通信的套接字!
		{
			pDlg->m_list.GetNext(pos);
			continue;
		}
		CClientSocket* client=(CClientSocket*)pDlg->m_list.GetNext(pos);//返回pos所指位置的套接字,并使pos++
		client->Send("M",2);
		client->Send(buffer,strlen(buffer));
	}

	}

	CSocket::OnReceive(nErrorCode);
}

void CClientSocket::OnClose(int nErrorCode) //关闭对话框
{
	// TODO: Add your specialized code here and/or call the base class
	CSeverApp* pApp=(CSeverApp*)AfxGetApp();
	CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;
	POSITION pos = pDlg->m_list.Find(this);
	if(pos != NULL)
	{
		pDlg->m_list.RemoveAt(pos);
	}
	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

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