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

📄 udpclientdlg.cpp

📁 学习嵌入式LINUX的好东西
💻 CPP
字号:
// UDPClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UDPClient.h"
#include "UDPClientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CUDPClientDlg dialog

CUDPClientDlg::CUDPClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUDPClientDlg::IDD, pParent)
	, m_LocalPort(23456)
	, m_RecvStr(_T(""))
	, m_SendStr(_T(""))
	, m_RemotePort(56789)
	, m_RemoteHost(_T("127.0.0.1"))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUDPClientDlg::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(CUDPClientDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_btnOpen, &CUDPClientDlg::OnBnClickedbtnopen)
	ON_BN_CLICKED(IDC_btnClose, &CUDPClientDlg::OnBnClickedbtnclose)
	ON_BN_CLICKED(IDC_btnSend, &CUDPClientDlg::OnBnClickedbtnsend)
END_MESSAGE_MAP()


// CUDPClientDlg message handlers

BOOL CUDPClientDlg::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 CUDPClientDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	DRA::RelayoutDialog(
		AfxGetInstanceHandle(), 
		this->m_hWnd, 
		DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_UDPCLIENT_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_UDPCLIENT_DIALOG));
}
#endif


void CALLBACK CUDPClientDlg::OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
	CUDPClientDlg * pDlg;
	pDlg = (CUDPClientDlg*)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 CUDPClientDlg::OnUdpCEError(CWnd * pWnd,int nError)
{

}



void CUDPClientDlg::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 CUDPClientDlg::OnBnClickedbtnclose()
{
	m_UDPSocket.Close();
	// TODO: Add your control notification handler code here
}

void CUDPClientDlg::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 + -