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

📄 tcpserverdlg.cpp

📁 在mobile下两个终端tcp无线连接
💻 CPP
字号:
// TCPServerDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "TCPServer.h"
#include "TCPServerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CTCPServerDlg 对话框

CTCPServerDlg::CTCPServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTCPServerDlg::IDD, pParent)
{
	m_localPort = 0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTCPServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDTLOCALPORT, m_localPort);
}

BEGIN_MESSAGE_MAP(CTCPServerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTNLISTEN, OnBtnlisten)
	ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
	ON_BN_CLICKED(IDC_BTNSEND, &CTCPServerDlg::OnBnClickedBtnsend)
END_MESSAGE_MAP()


// CTCPServerDlg 消息处理程序

BOOL CTCPServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	//设置默认值
	m_localPort = 5000;
	UpdateData(FALSE);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CTCPServerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	DRA::RelayoutDialog(
		AfxGetInstanceHandle(), 
		this->m_hWnd, 
		DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_TCPSERVER_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_TCPSERVER_DIALOG));
}
#endif


//客户端连接建立事件处理函数
void CALLBACK  CTCPServerDlg::OnClientConnect(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{
	CTCPServerDlg * pDlg = (CTCPServerDlg*)pWnd;
	CListBox * pLstConn = (CListBox*)pDlg->GetDlgItem(IDC_LSTCONN);
	ASSERT(pLstConn != NULL);
	pLstConn->AddString(pTcpCustom->m_RemoteHost + _T("建立连接"));
}
//客户端SOCKET关闭事件处理函数
void  CALLBACK CTCPServerDlg::OnClientClose(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{
	CTCPServerDlg * pDlg = (CTCPServerDlg*)pWnd;
	int iIndex;

	CListBox * pLstConn = (CListBox*)pDlg->GetDlgItem(IDC_LSTCONN);
	ASSERT(pLstConn != NULL);
	iIndex = pLstConn->FindString(iIndex,pTcpCustom->m_RemoteHost + _T("建立连接"));
	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_LSTRECV);
	ASSERT(pLstRecv != NULL);

	pLstRecv->AddString(_T("************************************"));
	pLstRecv->AddString(_T("来自: ") + pTcpCustom->m_RemoteHost );
	pLstRecv->AddString(_T("数据长度:")+strLen);
	pLstRecv->AddString(strRecv);
	if (!pTcpCustom->SendData("recv ok",strlen("recv ok")))
	{
		AfxMessageBox(_T("发送失败"));
	}
}

//客户端Socket错误事件处理函数
void CALLBACK CTCPServerDlg::OnClientError(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,int nErrorCode)
{

}

//服务器端Socket错误事件处理函数
void CALLBACK CTCPServerDlg::OnServerError(CWnd* pWnd,CTCPServer_CE* pTcpServer_CE,int nErrorCode)
{

}

//监听按钮单击事件方法
void CTCPServerDlg::OnBtnlisten() 
{
	UpdateData(TRUE);
	//设置m_tcpServer属性
	m_tcpServer.m_LocalPort = m_localPort;
	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("监听失败"));
		return;
	}
	CButton * pBtnListen = (CButton*)GetDlgItem(IDC_BTNLISTEN);
	ASSERT(pBtnListen != NULL);
	pBtnListen->EnableWindow(FALSE);

	CButton * pBtnClose = (CButton*)GetDlgItem(IDC_BTNCLOSE);
	ASSERT(pBtnClose != NULL);
	pBtnClose->EnableWindow(TRUE);	
}

//关闭按钮单击事件代码 
void CTCPServerDlg::OnBtnclose() 
{
	if (m_tcpServer.Close() <=0)
	{
		AfxMessageBox(_T("关闭TCP服务器失败"));
		return;
	}
	CButton * pBtnListen = (CButton*)GetDlgItem(IDC_BTNLISTEN);
	ASSERT(pBtnListen != NULL);
	pBtnListen->EnableWindow(TRUE);

	CButton * pBtnClose = (CButton*)GetDlgItem(IDC_BTNCLOSE);
	ASSERT(pBtnClose != NULL);
	pBtnClose->EnableWindow(FALSE);	

	CListBox * pLstConn = (CListBox*)GetDlgItem(IDC_LSTCONN);
	ASSERT(pLstConn != NULL);

	CListBox * pLstRecv = (CListBox*)GetDlgItem(IDC_LSTRECV);
	ASSERT(pLstRecv != NULL);

	pLstConn->ResetContent();
	pLstRecv->ResetContent();

}
void CTCPServerDlg::OnBnClickedBtnsend()
{
	// TODO: 在此添加控件通知处理程序代码
	/*CTCPCustom_CE* pTcpCustom;
	CString sendInfo;
	this->GetDlgItem(IDC_EDIT_INSERTINFO)->GetWindowText(sendInfo);
	if (!pTcpCustom->SendData("send from server",strlen("send from server")))
	{
		AfxMessageBox(_T("发送失败"));
	}*/
	m_tcpServer.OnClientRead = OnClientRead;
}
//服务器端发送到客户端的数据
void CALLBACK CTCPServerDlg::OnSentToServer(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,const char * buf,int len )
{
	if (!pTcpCustom->SendData("send from server",strlen("send from server")))
	{
		AfxMessageBox(_T("发送失败"));
	}
}

⌨️ 快捷键说明

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