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

📄 tcpsock.cpp

📁 电信机房MDF、电源柜监控源码,主要用在通信机房配线设备监控、电源柜监控
💻 CPP
字号:
// TCPSock.cpp : implementation file
//

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

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

//##ModelId=44B6F8830264
CTCPSock::CTCPSock()
{
	m_unPort = 8801 ;
	m_bRun = FALSE ;	//运行标志位
}

//##ModelId=44B6F8830265
CTCPSock::~CTCPSock()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CTCPSock member functions

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

		if(Accept(*sock))
		{
			sock->GetPeerName(strIp,unPort) ;
			sock->IsConnect = TRUE ;
			sock->nType = SOCK_CU ;//类型采集单元 
			sock->m_strIp = strIp ;
			sock->m_unPort = unPort ;
					
			pSock = (sSocket*)(theApp.m_pComm->m_pCommPool->GetSocket(strIp)) ;

			if( pSock != NULL )
			{
				if( pSock->bIsConnect )							
					delete (CClientSock*)pSock->pSock ;

				pSock->pSock = (LPVOID)sock ;
				pSock->strIP = strIp ;
				pSock->bIsConnect = TRUE ;
				strTip.Format("系统:接受 IP地址:%s 的 TCP 连接",strIp) ;

				theApp.OutPut(strTip) ;
				theApp.DebugShow(strTip) ;
				theApp.Show(strTip) ;
			}
			else
			{

				strTip.Format("异常:未知 IP %s 的 TCP 连接请求被拒绝",strIp ) ;
				theApp.OutPut(strTip) ;
				theApp.DebugShow(strTip) ;
				theApp.Show(strTip) ;
				sock->Close() ;
				delete sock ;
			}	
		}
		else
			delete sock ;
	}
	catch(_com_error e)
	{
		strTip.Format("异常:接收 IP: %s 的 TCP 连接时(OnAccept) 发生异常:%s",strIp,(LPCSTR)e.Description()) ;
		theApp.OutPut(strTip) ;
	}
	CAsyncSocket::OnAccept(nErrorCode);
}

//##ModelId=44B6F8830267
int CTCPSock::InitTCP(UINT unPort)
{
	CString strTip ;
	m_unPort = unPort ;
	try
	{
		if( !Create(m_unPort) )
		{
			strTip.Format( "异常:TCP 通讯端口 %d 创建失败",m_unPort) ;
			theApp.OutPut(strTip) ;
			return -1 ;
		}

		if( !Listen() )
		{
			strTip.Format( "异常:TCP 通讯端口 %d 监听失败",m_unPort) ;
			theApp.OutPut(strTip) ;
			return -1 ;
		}

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

		m_bRun = TRUE ;
		return 0 ;

	}
	catch(_com_error e)
	{
		strTip.Format("异常:初始化 TCP 端口 %d 发生异常:%s",m_unPort,(LPCSTR)e.Description()) ;
		theApp.Show(strTip) ;
		theApp.OutPut(strTip) ;
		return -1 ;
	}

}

//##ModelId=44B6F8830263
UINT CTCPSock::GetPort()
{
	return m_unPort ;
}

//##ModelId=44B6F8830262
int CTCPSock::CloseTCP()
{
	CString strTip ;
	Close() ;

	m_bRun = FALSE ;
	strTip.Format( "系统:TCP 通讯端口 %d 关闭" , m_unPort ) ;
	theApp.Show(strTip) ;
	//theApp.DebugShow(strTip) ;
	theApp.OutPut(strTip) ;

	return 0 ;
}


//##ModelId=44B6F8830253
BOOL CTCPSock::DelSock(CString strIp)
{
	CString strTip ;
	sSocket* pSock ;
	pSock = (sSocket*)(theApp.m_pComm->m_pCommPool->GetSocket(strIp)) ;

	if( pSock != NULL )
	{
		((CClientSock*)(pSock->pSock))->Close() ;
		delete ((CClientSock*)(pSock->pSock)) ;
		pSock->bIsConnect = FALSE ;
		pSock->pSock = NULL ;
		return TRUE ;
	}
	else
	{
		strTip.Format("未知IP: %s Socket CTCPSock::DelSock 关闭异常",strIp) ;
		theApp.OutPut(strTip) ;
		return FALSE ;
	}
}

⌨️ 快捷键说明

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