📄 udp_simulatordlg.cpp
字号:
// UDP_SimulatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDP_Simulator.h"
#include "UDP_SimulatorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDP_SimulatorDlg dialog
CUDP_SimulatorDlg::CUDP_SimulatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDP_SimulatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDP_SimulatorDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_UDP_Socket=NULL;
}
void CUDP_SimulatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUDP_SimulatorDlg)
DDX_Control(pDX, IDC_SAVE_FLAG, m_Save_Flag);
DDX_Control(pDX, ID_SEND_FILE_BUTTON, m_send_file_button);
DDX_Control(pDX, IDC_CHECK1, m_Hex_Mode);
DDX_Control(pDX, ID_SEND, m_send_button);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDP_SimulatorDlg, CDialog)
//{{AFX_MSG_MAP(CUDP_SimulatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnCreate)
ON_BN_CLICKED(ID_SEND, OnSend)
ON_BN_CLICKED(ID_OPEN_SEND_FILE_BUTTON, OnOpenSendFileButton)
ON_BN_CLICKED(ID_SEND_FILE_BUTTON, OnSendFileButton)
ON_BN_CLICKED(ID_OPEN_RECEIVE_FILE_BUTTON, OnOpenReceiveFileButton)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MSG_FROM_UDP_SOCKET,OnUDPSocketData)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDP_SimulatorDlg message handlers
BOOL CUDP_SimulatorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
m_send_button.EnableWindow(FALSE);
m_send_file_button.EnableWindow(FALSE);
{
CUDP udp;
SetDlgItemText(IDC_IPADDRESS1,udp.GetHostIP());
SetDlgItemText(IDC_IPADDRESS2,udp.GetHostIP());
SetDlgItemText(IDC_HOST_PORT,"999");
SetDlgItemText(IDC_CLIENT_PORT,"999");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CUDP_SimulatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUDP_SimulatorDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUDP_SimulatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUDP_SimulatorDlg::OnUDPSocketData(DWORD wParam, DWORD lParam)
{
static int counter=0;
UDPPackage *pPackage=(UDPPackage *)wParam;
CString str,str1;
int i,len;
unsigned char *pData=pPackage->pData;
len=pPackage->len;
str.Format("[%4d %11s:%4d] ",counter++,pPackage->ip,pPackage->port);
if(m_Hex_Mode.GetCheck())
{
for(i=0;i<len;i++)
{
str1.Format("%02X ",pData[i]&0x0ff);
str+=str1;
}
} else
{
pData[len]=0;
str1.Format("%s",pData);
str+=str1;
}
SetDlgItemText(IDC_RECEIVE_DATA,str);
if(m_Save_Flag.GetCheck())
{
FILE *fp;
char name[256];
GetDlgItemText(IDC_RECEIVE_FILE,name,256);
if(fp=fopen(name,"rb"))
{
fclose(fp);
fp=fopen(name,"wb");
} else fp=fopen(name,"wb");
if(fp)
{
fwrite(pData,1,len,fp);
fclose(fp);
}
}
}
void CUDP_SimulatorDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_UDP_Socket)
{
m_UDP_Socket->Close();
delete m_UDP_Socket;
}
CDialog::OnCancel();
}
void CUDP_SimulatorDlg::OnCreate()
{
// TODO: Add your control notification handler code here
int port;
port=GetDlgItemInt(IDC_HOST_PORT);
if(m_UDP_Socket)
{
m_UDP_Socket->Close();
delete m_UDP_Socket;
m_UDP_Socket=NULL;
}
m_UDP_Socket=new CUDP(this,WM_MSG_FROM_UDP_SOCKET);
if(!m_UDP_Socket->Create(port,SOCK_DGRAM))
{
delete m_UDP_Socket;
m_UDP_Socket=NULL;
AfxMessageBox("端口创建失败!");
return;
} else AfxMessageBox("端口创建成功!");
m_send_button.EnableWindow();
m_send_file_button.EnableWindow();
}
void CUDP_SimulatorDlg::OnSend()
{
// TODO: Add your control notification handler code here
int port;
char sbuf[256];
port=GetDlgItemInt(IDC_CLIENT_PORT);
CString ip;
GetDlgItemText(IDC_IPADDRESS2,ip);
GetDlgItemText(IDC_SEND_DATA,sbuf,256);
if(m_UDP_Socket)
{
m_UDP_Socket->SendTo(sbuf,strlen(sbuf),port,ip);
}
}
void CUDP_SimulatorDlg::OnOpenSendFileButton()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if(dlg.DoModal()!=IDOK) return;
SetDlgItemText(IDC_SEND_FILE,dlg.GetPathName());
}
void CUDP_SimulatorDlg::OnSendFileButton()
{
// TODO: Add your control notification handler code here
int port;
port=GetDlgItemInt(IDC_CLIENT_PORT);
CString ip;
GetDlgItemText(IDC_IPADDRESS2,ip);
if(m_UDP_Socket)
{
CString name;
GetDlgItemText(IDC_SEND_FILE,name);
CFile file;
if(file.Open(name,CFile::modeRead|CFile::typeBinary|CFile::shareDenyNone))
{
char *pBuffer;
pBuffer=new char[min(32767,file.GetLength())];
file.Read(pBuffer,min(32767,file.GetLength()));
m_UDP_Socket->SendTo(pBuffer,min(32767,file.GetLength()),port,ip);
delete pBuffer;
file.Close();
}
}
}
void CUDP_SimulatorDlg::OnOpenReceiveFileButton()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if(dlg.DoModal()!=IDOK) return;
SetDlgItemText(IDC_RECEIVE_FILE,dlg.GetPathName());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -