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

📄 clientsock.cpp

📁 MDF监控源码2
💻 CPP
字号:
// ClientSock.cpp : implementation file
//

#include "stdafx.h"
#include "mdf.h"
#include "ClientSock.h"
#include "MDFDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientSock

extern CMDFApp theApp ;

//##ModelId=44B6F88702EF
CClientSock::CClientSock()
{
	//m_hRead = CreateEvent ( NULL , FALSE , TRUE , NULL ) ; //建立信号状态
	m_hWrite = CreateEvent ( NULL , FALSE , TRUE , NULL ) ; //建立信号状态
	memset(ucRecvBuf,0,sizeof(ucRecvBuf)) ;

	IsConnect = FALSE ;
	nType = 0 ; //设备 -- 0,WEB -- 1
	unRecvLen = 0 ;
}

//##ModelId=44B6F88702F0
CClientSock::~CClientSock()
{
	CloseHandle(m_hWrite) ;
}


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

/////////////////////////////////////////////////////////////////////////////
// CClientSock member functions

//##ModelId=44B6F887031D
void CClientSock::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	int len ;
	//UINT unResult ;
	UCHAR buf[4096] ;

	CString strIp,strSource ;
	CString strData,strTip,strStep ;
	
	try
	{
		memset(buf,0,sizeof(buf)) ;

		len = Receive(buf,sizeof(buf)) ;

		if( len <= 0 )
			return ;
		
		//对来自Web端的数据进行处理
		if( nType == SOCK_WEB )
		{
			strData = buf ;
			theApp.m_pComm->WebRecv(strData,(LPVOID)this) ;
		}
		else
			theApp.m_pComm->RecvData(buf,len,ucRecvBuf,&unRecvLen,m_strIp) ;

	}
	catch(_com_error e)
	{
		strTip.Format("接收 IP: %s 的 Socket 通讯时(OnReceive) 发生异常:%s",m_strIp,(LPCSTR)e.Description()) ;
		theApp.Show(strTip) ;
	}

	CAsyncSocket::OnReceive(nErrorCode);

}

//##ModelId=44B6F8870320
void CClientSock::OnClose(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CloseClient() ;
	CAsyncSocket::OnClose(nErrorCode);
}

//##ModelId=44B6F8870300
int CClientSock::SendData(CString strSend)
{
	int nNum ;
	CString strIp,strTip ;

	nNum = 0 ;

	if( IsConnect )
	{

		if( nType == SOCK_WEB )//Web端采用UNICODE发送
			nNum = Send( strSend.AllocSysString(),(strSend.GetLength()+1)*2 ) ;
		else
			nNum = Send( strSend.GetBuffer(0),strSend.GetLength() ) ;

	}

	return nNum ;

}

//##ModelId=44B6F88702FF
BOOL CClientSock::GetConnect()
{
	return IsConnect ;
}

//##ModelId=44B6F88702FE
 void CClientSock::CloseClient()
{
	UINT unPort ;
	CString strTip ;
	CString strIp ;
	IsConnect = FALSE ;

	GetPeerName(strIp,unPort) ;

	if( theApp.m_pComm->m_pCommPool->m_Web.GetIP() == strIp && nType == SOCK_WEB )
	{
		theApp.m_pComm->m_pCommPool->m_Web.DelSock(this) ;
		strTip.Empty() ;
		//strTip.Format("系统:Web服务器 IP:%s 连接断开",strIp ) ;
	}
	else
	{
		strTip.Format("系统:IP:%s 连接断开",strIp ) ;
		theApp.m_pComm->m_pCommPool->m_TCP.DelSock(strIp) ;
	}

	theApp.Show(strTip) ;
	
}

⌨️ 快捷键说明

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