📄 tcpserverdlg.cpp
字号:
// TCPServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TCPServer.h"
#include "TCPServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTCPServerDlg dialog
CTCPServerDlg::CTCPServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTCPServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCPServerDlg)
m_nServerPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTCPServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCPServerDlg)
DDX_Text(pDX, IDC_EDIT_SERVERPORT, m_nServerPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTCPServerDlg, CDialog)
//{{AFX_MSG_MAP(CTCPServerDlg)
ON_BN_CLICKED(IDC_BTN_LISTEN, OnBtnListen)
ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPServerDlg message handlers
BOOL CTCPServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
m_nServerPort = 5000;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// 客户端连接建立事件处理函数
void CALLBACK CTCPServerDlg::OnClientConnect(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{
CTCPServerDlg * pDlg = (CTCPServerDlg*)pWnd;
CListBox * pListConn = (CListBox*)pDlg->GetDlgItem(IDC_LIST_CLIENT);
ASSERT(pListConn != NULL);
pListConn->AddString(pTcpCustom->m_RemoteHost + _T("Connection was build"));// 建立连接
}
// 客户端SOCKET关闭事件处理函数
void CALLBACK CTCPServerDlg::OnClientClose(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{
CTCPServerDlg * pDlg = (CTCPServerDlg *)pWnd;
CListBox * pLstConn = (CListBox*)pDlg->GetDlgItem(IDC_LIST_CLIENT);
ASSERT(pLstConn != NULL);
int iIndex = 0;
iIndex = pLstConn->FindString(iIndex,pTcpCustom->m_RemoteHost + _T("Connection was build"));// 建立连接
if (iIndex == LB_ERR)
{
return;
}
pLstConn->DeleteString(iIndex);
}
//服务器端收到来自客户端的数据
void CALLBACK CTCPServerDlg::OnClientRead(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,const char * buf,int len )
{
CString strRecv;
CString strLen;
strLen.Format(L"%d",len);
strRecv = buf;
CTCPServerDlg * pDlg = (CTCPServerDlg*)pWnd;
CListBox * pLstRecv = (CListBox*)pDlg->GetDlgItem(IDC_LIST_RECVDATA);
ASSERT(pLstRecv != NULL);
pLstRecv->AddString(_T("****"));
pLstRecv->AddString(_T("Come From: ") + pTcpCustom->m_RemoteHost );
pLstRecv->AddString(_T("Data Length: ")+strLen);
pLstRecv->AddString(strRecv);
if (!pTcpCustom->SendData("recv ok",strlen("recv ok")))
{
AfxMessageBox(_T("Send data failed")); // 发送失败
}
}
// 客户端Socket错误事件处理函数
void CALLBACK CTCPServerDlg::OnClientError(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,int nErrorCode)
{
AfxMessageBox(_T("Client Socket Error!"));
}
// 服务器端Socket错误事件处理函数
void CALLBACK CTCPServerDlg::OnServerError(CWnd* pWnd,CTCPServer_CE* pTcpServer_CE,int nErrorCode)
{
AfxMessageBox(_T("Server Socket Error!"));
}
// 监听按钮单击事件方法
void CTCPServerDlg::OnBtnListen()
{
UpdateData(TRUE);
//设置m_tcpServer属性
m_tcpServer.m_LocalPort = m_nServerPort;
m_tcpServer.m_pOwnerWnd = this;
m_tcpServer.OnClientConnect = OnClientConnect;
m_tcpServer.OnClientClose = OnClientClose;
m_tcpServer.OnClientRead = OnClientRead;
m_tcpServer.OnClientError = OnClientError;
m_tcpServer.OnServerError = OnServerError;
if (m_tcpServer.Open() <= 0)
{
AfxMessageBox(_T("Listen failed")); // 监听失败
return;
}
}
// 关闭按钮单击事件代码
void CTCPServerDlg::OnBtnClose()
{
if (m_tcpServer.Close() <=0)
{
AfxMessageBox(_T("Close TCP Server failed"));// 关闭TCP服务器失败
return;
}
CListBox * pLstConn = (CListBox*)GetDlgItem(IDC_LIST_CLIENT);
ASSERT(pLstConn != NULL);
pLstConn->ResetContent();
CListBox * pLstRecv = (CListBox*)GetDlgItem(IDC_LIST_CLIENT);
ASSERT(pLstRecv != NULL);
pLstRecv->ResetContent();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -