📄 udpdemodlg.cpp
字号:
// UDPDemoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "UDPDemo.h"
#include "UDPDemoDlg.h"
#include "udp_ce.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CUDPDemoDlg 对话框
CUDPDemoDlg::CUDPDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDPDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDPDemoDlg)
m_LocalPort = 0;
m_RecvNum = 0;
m_RecvStr = _T("");
m_RemoteHost = _T("");
m_RemotePort = 0;
m_SendNum = 0;
m_SendStr = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDPDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUDPDemoDlg)
DDX_Text(pDX, IDC_EDIT_LOCALPORT, m_LocalPort);
DDX_Text(pDX, IDC_EDIT_RECVNUM, m_RecvNum);
DDX_Text(pDX, IDC_EDIT_RECVSTR, m_RecvStr);
DDX_Text(pDX, IDC_EDIT_REMOTEHOST, m_RemoteHost);
DDX_Text(pDX, IDC_EDIT_REMOTEPORT, m_RemotePort);
DDX_Text(pDX, IDC_EDIT_SENDNUM, m_SendNum);
DDX_Text(pDX, IDC_EDIT_SENDSTR, m_SendStr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDPDemoDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BTNOPEN, &CUDPDemoDlg::OnBnClickedBtnopen)
ON_BN_CLICKED(IDC_BTNCLOSE, &CUDPDemoDlg::OnBnClickedBtnclose)
ON_BN_CLICKED(IDC_BTNSEND, &CUDPDemoDlg::OnBnClickedBtnsend)
END_MESSAGE_MAP()
// CUDPDemoDlg 消息处理程序
BOOL CUDPDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CUDPDemoDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_UDPDEMO_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_UDPDEMO_DIALOG));
}
#endif
void CALLBACK CUDPDemoDlg::OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
CUDPDemoDlg * pDlg;
pDlg = (CUDPDemoDlg*)pWnd;
char * recvStr;
int recvNum;
recvStr = new char(nLen-4);
//解析出收到的字符串
CopyMemory(recvStr,buf,nLen-4);
//解析出收到的数字
CopyMemory(&recvNum,buf+nLen-4,4);
//在字符串接收框中,显示收到的字符串
CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_RECVSTR);
ASSERT(pRecvStrEdit != NULL);
CString strRecv = (CString)recvStr;
pRecvStrEdit->SetWindowText(strRecv);
//在数字接收框中,显示接收到的数字
CEdit *pRecvNumEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_RECVNUM);
ASSERT(pRecvNumEdit != NULL);
CString numRecv;
numRecv.Format(L"%d",recvNum);
pRecvNumEdit->SetWindowText(numRecv);
//删除动态内存
delete[] recvStr;
recvStr = NULL;
}
void CALLBACK CUDPDemoDlg::OnUdpCEError(CWnd * pWnd,int nError)
{
}
void CUDPDemoDlg::OnBnClickedBtnopen()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_CEUdp.m_OnUdpRecv = OnUdpCERecv;
m_CEUdp.m_OnUdpError = OnUdpCEError;
DWORD nResult = m_CEUdp.Open(this,m_LocalPort,m_RemoteHost.GetBuffer(m_RemoteHost.GetLength()),m_RemotePort);
if (nResult <=0)
{
AfxMessageBox(_T("打开端口失败"));
}
char hostName[255];
ZeroMemory(hostName,255);
gethostname(hostName,255);
}
void CUDPDemoDlg::OnBnClickedBtnclose()
{
// TODO: 在此添加控件通知处理程序代码
m_CEUdp.Close();
}
void CUDPDemoDlg::OnBnClickedBtnsend()
{
// TODO: 在此添加控件通知处理程序代码
char * sendBuf;
int sendLen=0;
UpdateData(TRUE);
sendLen = m_SendStr.GetLength()+4;
sendBuf = new char(sendLen);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,m_SendStr.GetBuffer(m_SendStr.GetLength())
,m_SendStr.GetLength(),sendBuf,m_SendStr.GetLength(),NULL,NULL);
CopyMemory(sendBuf+m_SendStr.GetLength(),&m_SendNum,4);
m_CEUdp.SendData(sendBuf,sendLen);
delete[] sendBuf;
sendBuf = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -