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

📄 websock.cpp

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

#include "stdafx.h"
#include "MDF.h"
#include "WebSock.h"
#include "ClientSock.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CMDFApp theApp ;
/////////////////////////////////////////////////////////////////////////////
// CWebSock

//##ModelId=44B6F88300FB
CWebSock::CWebSock()
{
	m_pSock = NULL ;
	m_bRun = FALSE ;
}

//##ModelId=44B6F88300FC
CWebSock::~CWebSock()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CWebSock member functions

//##ModelId=44B6F8830157
void CWebSock::OnAccept(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	UINT unPort ;
	CString strIp,strTip ;
	CClientSock* sock ;

	sock = new CClientSock ;

	if(Accept(*sock))
	{
		sock->GetPeerName(strIp,unPort) ;

		if( m_strIp ==  strIp )
		{
			sock->nType = SOCK_WEB ; //Web--1
			sock->IsConnect = TRUE ;

			m_ListSock.AddTail((LPVOID)sock) ;

			strTip.Format("系统:Web服务器 IP:%s 成功连接监测中心",strIp ) ;
		}
		else
		{
			strTip.Format("异常:非法 Web IP:%s 连接请求被拒绝",strIp ) ;
			theApp.OutPut(strTip) ;	

			if( theApp.m_pComm->m_bIsDebug )
			{
				strTip += "\r\n" ;
				theApp.DebugShow(strTip) ;
			}

			theApp.Show(strTip) ;
			delete sock ;
		}
	}

	CAsyncSocket::OnAccept(nErrorCode) ;
}

//##ModelId=44B6F883011B
int CWebSock::InitWeb(UINT unPort,CString strIp)
{
	CString strTip ;
	
	try
	{
		m_unPort = unPort ;
		m_strIp.Empty() ;
		m_strIp = strIp ;

		if( !Create(m_unPort) )
		{
			strTip.Format( "系统:Web 通讯端口 %d 创建失败",m_unPort) ;
			theApp.OutPut(strTip) ;
			return -1 ;
		}
		if( !Listen() )
		{
			strTip.Format( "系统:Web 通讯端口 %d 监听失败",m_unPort) ;
			theApp.OutPut(strTip) ;
			return -1 ;
		}

		strTip.Format( "系统:Web 通讯端口 %d 开启", m_unPort ) ;
		theApp.Show(strTip) ;
		theApp.OutPut(strTip) ;
		strTip = "* " + strTip ;
		theApp.DebugShow(strTip) ;

		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format("异常:初始化Web服务器 发生异常:%s",(LPCSTR)e.Description()) ;
		theApp.OutPut(strTip) ;
		return -1 ;
	}
}

//##ModelId=44B6F883011A
BOOL CWebSock::GetConnect()
{
	return FALSE ;
}

//##ModelId=44B6F8830119
CString CWebSock::GetIP()
{
	return m_strIp ;
}

//##ModelId=44B6F883010E
UINT CWebSock::GetPort()
{
	return m_unPort ;
}

//##ModelId=44B6F883010D
BOOL CWebSock::GetRun()
{
	return m_bRun ;
}

//##ModelId=44B6F883010C
int CWebSock::CloseWeb()
{
	int i ;
	CString strTip ;
	CClientSock* pSock ;

	try
	{

		strTip.Format( "系统:Web 通讯端口 %d 关闭", m_unPort ) ;
		theApp.Show(strTip) ;
		theApp.OutPut(strTip) ;
		Close() ;//关闭监听Socket

		Sleep(100) ;

		if( !m_ListSock.IsEmpty() )
		{
			POSITION pos ;

			for( i = 0 ; i < m_ListSock.GetCount() ; i++ )
			{
				pos = m_ListSock.FindIndex(i) ;
				pSock = (CClientSock*)m_ListSock.GetAt(pos) ;
				if( pSock != NULL )
				{
					if( pSock->IsConnect )
						pSock->CloseClient() ;
				}
			}

			Sleep(100) ;

			for( i = 0 ; i < m_ListSock.GetCount() ; i++ )
			{
				pos = m_ListSock.FindIndex(i) ;
				pSock = (CClientSock*)m_ListSock.GetAt(pos) ;

				if( pSock != NULL )
				{
					if( pSock->IsConnect )
						delete pSock ;
				}

			}
			m_ListSock.RemoveAll() ;
		}

		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format("异常:关闭Web服务时 发生异常:%s",(LPCSTR)e.Description()) ;
		theApp.OutPut(strTip) ;
		return -1 ;
	}
}

//##ModelId=44B6F883010B
int CWebSock::DelClientSock()
{
 	CString strTip ;
	try
	{
		int i ;
		LPVOID pSock ;

		if( m_ListSock.IsEmpty() )
			return 0 ;

		POSITION pos = m_ListSock.GetHeadPosition() ;
		for( i=0 ; i < m_ListSock.GetCount() ; i++ )
		{
			pos = m_ListSock.FindIndex(i) ;
			pSock = m_ListSock.GetAt(pos) ;
			if( !((CClientSock*)pSock)->GetConnect() )
			{
				delete (CClientSock*)pSock ;
				m_ListSock.RemoveAt(pos) ;
				i-- ;
			}

		}

		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format("异常:关闭Web服务时 发生异常:%s",(LPCSTR)e.Description()) ;
		theApp.OutPut(strTip) ;
		return -1 ;
	}
}

//##ModelId=44B6F8830109
BOOL CWebSock::DelSock(LPVOID lpSock)
{
	int i ;
	LPVOID pSock ;
	BOOL bResult ;
	bResult = FALSE ;

	if( m_ListSock.IsEmpty() )
		return	bResult ;

	POSITION pos = m_ListSock.GetHeadPosition() ;
	for( i=0 ; i < m_ListSock.GetCount() ; i++ )
	{
		pos = m_ListSock.FindIndex(i) ;
		pSock = m_ListSock.GetAt(pos) ;

		if( pSock == lpSock )
		{
			((CClientSock*)pSock)->Close() ;
			delete (CClientSock*)pSock ;
			m_ListSock.RemoveAt(pos) ;
			bResult = TRUE ;
			break ;
		}	
	}
	return bResult ;
}

//##ModelId=44B6F88300FE
BOOL CWebSock::WebSend(LPVOID lpSock,CString strSend)
{
	int i ;
	LPVOID pSock ;
	BOOL bResult ;
	bResult = FALSE ;

	if( m_ListSock.IsEmpty() )
		return	bResult ;

	POSITION pos = m_ListSock.GetHeadPosition() ;
	for( i=0 ; i < m_ListSock.GetCount() ; i++ )
	{
		pos = m_ListSock.FindIndex(i) ;
		pSock = m_ListSock.GetAt(pos) ;

		if( pSock == lpSock )
		{
			((CClientSock*)pSock)->SendData(strSend) ;
			bResult = TRUE ;
			break ;
		}	
	}
	return bResult ;
}

⌨️ 快捷键说明

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