?? tcpsock.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 + -