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

📄 udpdlg.cpp

📁 代码原本出自《EVC高级编程及其应用开发》
💻 CPP
字号:
// UDPDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "UDP.h"
#include "UDPDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CUDPDlg 对话框

CUDPDlg::CUDPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUDPDlg::IDD, pParent)
	, m_RemoteHost(_T("172.19.37.13"))
	, m_RemotePort(9000)
	, m_LocalPort(9001)
	, m_SendStr(_T("test test test"))
	, m_RecvStr(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUDPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_REMOTEHOST, m_RemoteHost);
	DDX_Text(pDX, IDC_EDIT_REMOTEPORT, m_RemotePort);
	DDX_Text(pDX, IDC_EDIT_LOCALPORT, m_LocalPort);
	DDX_Text(pDX, IDC_EDIT_SENDSTR, m_SendStr);
	DDX_Text(pDX, IDC_EDIT_RECVSTR, m_RecvStr);
}

BEGIN_MESSAGE_MAP(CUDPDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTNOPEN, &CUDPDlg::OnBnClickedBtnopen)
	ON_BN_CLICKED(IDC_BTNCLOSE, &CUDPDlg::OnBnClickedBtnclose)
	ON_BN_CLICKED(IDC_BTNSEND, &CUDPDlg::OnBnClickedBtnsend)
END_MESSAGE_MAP()


// CUDPDlg 消息处理程序

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

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

	// TODO: 在此添加额外的初始化代码
	m_bFullScreen=false;
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CALLBACK CUDPDlg::OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
	CUDPDlg * pDlg;
	pDlg = (CUDPDlg*)pWnd;
	CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_RECVSTR);
	ASSERT(pRecvStrEdit != NULL);
	CString strRecv (buf);
	pRecvStrEdit->SetWindowText(strRecv);
}

void CALLBACK CUDPDlg::OnUdpCEError(CWnd * pWnd,int nError)
{
   
}


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


void CUDPDlg::OnBnClickedBtnopen()
{
   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("打开端口失败"));
	   return;
   }
}

void CUDPDlg::OnBnClickedBtnclose()
{
  m_CEUdp.Close();	
}

void CUDPDlg::OnBnClickedBtnsend()
{
   UpdateData(TRUE);
   char *pBuffer = NULL;
   pBuffer = new char[m_SendStr.GetLength()+1];
   ZeroMemory(pBuffer, m_SendStr.GetLength()+1); 
   USES_CONVERSION;
   pBuffer=(char*)W2A((LPCTSTR)m_SendStr); 
   m_CEUdp.SendData(pBuffer,strlen(pBuffer));	
  delete[] pBuffer;
}

⌨️ 快捷键说明

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