📄 udpdemodlg.cpp
字号:
// UDPDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDPDemo.h"
#include "UDPDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUDPDemoDlg dialog
CUDPDemoDlg::CUDPDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDPDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDPDemoDlg)
m_strRemoteIP = _T("192.168.10.1");
m_nLocalPort = 3456;
m_nRemotePort = 2345;
m_strRecv = _T("");
m_strSend = _T("abcdefgh");
//}}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_IP, m_strRemoteIP);
DDX_Text(pDX, IDC_EDIT_LPORT, m_nLocalPort);
DDX_Text(pDX, IDC_EDIT_RPORT, m_nRemotePort);
DDX_Text(pDX, IDC_EDIT_RECV, m_strRecv);
DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDPDemoDlg, CDialog)
//{{AFX_MSG_MAP(CUDPDemoDlg)
ON_BN_CLICKED(IDC_BTNOPEN, OnBtnOpen)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnClose)
ON_BN_CLICKED(IDC_BTNSEND, OnBtnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDPDemoDlg message handlers
BOOL CUDPDemoDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CUDPDemoDlg::OnBtnOpen()
{
UpdateData(TRUE);
m_CEUdp.m_OnUdpRecv = OnUdpCERecv;
m_CEUdp.m_OnUdpError = OnUdpCEError;
DWORD nResult = m_CEUdp.Open(this,m_nLocalPort,m_strRemoteIP.GetBuffer(m_strRemoteIP.GetLength()),m_nRemotePort);
if (nResult <=0)
{
AfxMessageBox(_T("Open Port Failed!"));
}
char hostName[255];
ZeroMemory(hostName, 255);
gethostname(hostName, 255);
}
void CUDPDemoDlg::OnBtnClose()
{
m_CEUdp.Close();
}
void CUDPDemoDlg::OnBtnSend()
{
char * sendBuf;
int sendLen = 0;
UpdateData(TRUE);
sendLen = m_strSend.GetLength();
sendBuf = new char[sendLen];
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, m_strSend.GetBuffer(m_strSend.GetLength()),
m_strSend.GetLength(),sendBuf,m_strSend.GetLength(),NULL,NULL);
m_CEUdp.SendData(sendBuf, sendLen);
delete[] sendBuf;
sendBuf = NULL;
}
void CALLBACK CUDPDemoDlg::OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
CUDPDemoDlg * pDlg;
pDlg = (CUDPDemoDlg*)pWnd;
char * recvStr = NULL;
// 接收的数据
recvStr = new char[nLen];
ZeroMemory(recvStr, nLen);
CopyMemory(recvStr,buf,nLen);
// 显示收到的数据
CWnd *pRecvStrEdit = (CWnd*)pDlg->GetDlgItem(IDC_EDIT_RECV);
ASSERT(pRecvStrEdit != NULL);
CString strRecv = recvStr;
pRecvStrEdit->SetWindowText(strRecv);
// 删除动态内存
delete[] recvStr;
recvStr = NULL;
}
void CALLBACK CUDPDemoDlg::OnUdpCEError(CWnd * pWnd,int nError)
{
AfxMessageBox(_T("error!"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -