📄 udpserverdlg.cpp
字号:
// UDPServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDPServer.h"
#include "UDPServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CUDPServerDlg dialog
CUDPServerDlg::CUDPServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDPServerDlg::IDD, pParent)
, m_LocalPort(56789)
, m_RecvStr(_T(""))
, m_SendStr(_T(""))
, m_RemotePort(23456)
, m_RemoteHost(_T("127.0.0.1"))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDPServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_LOCALPORT, m_LocalPort);
DDX_Text(pDX, IDC_EDIT_SENDSTR, m_SendStr);
DDX_Text(pDX, IDC_EDIT_RECVSTR, m_RecvStr);
DDX_Text(pDX, IDC_EDIT_REMOTEPORT, m_RemotePort);
DDX_Text(pDX, IDC_EDIT_REMOTEHOST, m_RemoteHost);
}
BEGIN_MESSAGE_MAP(CUDPServerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_btnOpen, &CUDPServerDlg::OnBnClickedbtnopen)
ON_BN_CLICKED(IDC_btnClose, &CUDPServerDlg::OnBnClickedbtnclose)
ON_BN_CLICKED(IDC_btnSend, &CUDPServerDlg::OnBnClickedbtnsend)
END_MESSAGE_MAP()
// CUDPServerDlg message handlers
BOOL CUDPServerDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CUDPServerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_UDPSERVER_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_UDPSERVER_DIALOG));
}
#endif
void CALLBACK CUDPServerDlg::OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
CUDPServerDlg * pDlg;
pDlg = (CUDPServerDlg*)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(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 CUDPServerDlg::OnUdpCEError(CWnd * pWnd,int nError)
{
}
void CUDPServerDlg::OnBnClickedbtnopen()
{
UpdateData(TRUE);
m_UDPSocket.m_OnUdpRecv = OnUdpCERecv;
m_UDPSocket.m_OnUdpError = OnUdpCEError;
DWORD nResult = m_UDPSocket.Open(this,m_LocalPort,m_RemoteHost.GetBuffer(m_RemoteHost.GetLength()),m_RemotePort);
if (nResult <=0)
{
AfxMessageBox(_T("Open Port Fail"));
}
char hostName[255];
ZeroMemory(hostName,255);
gethostname(hostName,255);
}
void CUDPServerDlg::OnBnClickedbtnclose()
{
m_UDPSocket.Close();
// TODO: Add your control notification handler code here
}
void CUDPServerDlg::OnBnClickedbtnsend()
{
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_UDPSocket.SendData(sendBuf,sendLen);
delete[] sendBuf;
sendBuf = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -