📄 udptestdlg.cpp
字号:
// UDPTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDPTest.h"
#include "UDPTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MSG_PRINTCONSOLE WM_USER + 334
/////////////////////////////////////////////////////////////////////////////
// CUDPTestDlg dialog
void PrintConsole(char *format,...);
CUDPTestDlg::CUDPTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDPTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDPTestDlg)
m_IPAddress = _T("172.19.18.51");
m_LocalAddress = _T("172.19.18.51");
m_Port = 7543;
m_LocalPort = 7543;
m_AnyPort = FALSE;
m_bListenIP = FALSE;
m_ListenIPAddress = _T("172.19.18.51");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDPTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUDPTestDlg)
DDX_Control(pDX, IDC_STOP, m_StopBtn);
DDX_Control(pDX, IDC_START, m_StartBtn);
DDX_Control(pDX, IDE_MSG, m_hMsg);
DDX_Text(pDX, IDC_IPADDR, m_IPAddress);
DDX_Text(pDX, IDC_LOCALIPADDR, m_LocalAddress);
DDX_Text(pDX, IDC_PORT, m_Port);
DDX_Text(pDX, IDC_LOCALPORT, m_LocalPort);
DDX_Check(pDX, IDC_CHECK1, m_AnyPort);
DDX_Check(pDX, IDC_CHKLESTEN, m_bListenIP);
DDX_Text(pDX, IDC_LISTENIP, m_ListenIPAddress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDPTestDlg, CDialog)
//{{AFX_MSG_MAP(CUDPTestDlg)
ON_MESSAGE(MSG_PRINTCONSOLE,OnPrintConsole)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SEND, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDPTestDlg message handlers
void CUDPTestDlg::OnPrintConsole(WPARAM wparam,LPARAM lparam)
{
int slen = strlen((char*) lparam );
char *pBuff = new char[slen +2];
if( NULL == pBuff )
return;
strcpy( pBuff, (char *)lparam );
if('\n' == pBuff[slen -1 ] )
{
*(pBuff + slen - 1) = 0x0d;
*(pBuff + slen ) = 0x0a;
*(pBuff + slen + 1) = '\0';
}
int linecount = m_hMsg.GetLineCount();
if(linecount>200)
{
m_hMsg.SetSel(0,m_hMsg.GetWindowTextLength ());
}
else
{
m_hMsg.SetSel(m_hMsg.GetWindowTextLength (),-1);
}
m_hMsg.ReplaceSel (pBuff);
delete pBuff;
return;
}
BOOL CUDPTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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 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 CUDPTestDlg::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();
}
}
HCURSOR CUDPTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void PrintConsole(char *format,...)
{
CWnd *fMainWnd = NULL;
fMainWnd = AfxGetApp()->m_pMainWnd;
if (! fMainWnd)
{
return;
}
CString MsgStr= "";
va_list args;
va_start(args, format);
MsgStr.FormatV(format, args);
CString tmpstr;
char *fInfo = NULL;
time_t now = time(0);
struct tm *ts = localtime(&now);
if (!ts) return ;
tmpstr.Format ("%02d:%02d:%02d ",ts->tm_hour, ts->tm_min, ts->tm_sec );
MsgStr = tmpstr + MsgStr + "\n";
fMainWnd->SendMessage (MSG_PRINTCONSOLE,(WPARAM) 0,(LPARAM)(LPCTSTR)MsgStr );
}
void CUDPTestDlg::OnStart()
{
// TODO: Add your control notification handler code here
WORD wVersionRequested ;
WSADATA wsaData ;
wVersionRequested = MAKEWORD( 2, 0 ) ;
if( WSAStartup( wVersionRequested, &wsaData ) != 0 )
{
PrintConsole("WSAStartup() initialize fail!\n");
return;
}
UpdateData(true);
socketID = ::socket( AF_INET, SOCK_DGRAM, 0 ) ;
int recvbuff = 128*1024 ;
int sendbuff = 128*1024 ;
setsockopt( socketID, SOL_SOCKET, SO_RCVBUF, (char *)&recvbuff, sizeof(recvbuff) ) ;
setsockopt( socketID, SOL_SOCKET, SO_SNDBUF, (char *)&sendbuff, sizeof(sendbuff) ) ;
unsigned long icmd = 1;
#ifdef WIN32
BOOL bOption = FALSE ;
if( SOCKET_ERROR == setsockopt( socketID,SOL_SOCKET,SO_REUSEADDR,(char *)&bOption, sizeof(BOOL) ))
errNo = WSAGetLastError();
bOption = TRUE ;
if ( SOCKET_ERROR==setsockopt( socketID,SOL_SOCKET,SO_BROADCAST,(char *)&bOption,sizeof(BOOL) ) )
errNo = WSAGetLastError();
ioctlsocket( socketID, FIONBIO, &icmd ) ;
#else
int on =0;
setsockopt(socketID,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
on = 1;
setsockopt( socketID,SOL_SOCKET,SO_BROADCAST,(char *)&on,sizeof(on));
ioctl( socketID, FIONBIO, &icmd ) ;
#endif
sockaddr_in address ;
memset( &address , 0 , sizeof(sockaddr_in) ) ;
address.sin_family = AF_INET;
if(m_AnyPort)
address.sin_port = INADDR_ANY;
else
address.sin_port = htons(m_LocalPort) ;
address.sin_addr.s_addr = INADDR_ANY ;
if( ::bind (socketID, (struct sockaddr *)&address, sizeof(sockaddr)) < 0 )
{
MessageBox("Open UDP fail! Please check IP&Port!");
OnStop( ) ;
return ;
}
m_sin_listen_addr.s_addr = inet_addr(m_ListenIPAddress) ;
PrintConsole("Open UDP OK!");
m_StartBtn.ShowWindow(SW_HIDE);
m_StopBtn.ShowWindow(SW_SHOW);
SetTimer(1,500,NULL);
CEdit *thePort = (CEdit*)GetDlgItem(IDC_LOCALPORT);
thePort->EnableWindow(false);
thePort = (CEdit*)GetDlgItem(IDC_PORT);
thePort->EnableWindow(false);
thePort = (CEdit*)GetDlgItem(IDC_LOCALIPADDR);
thePort->EnableWindow(false);
thePort = (CEdit*)GetDlgItem(IDC_IPADDR);
thePort->EnableWindow(false);
}
void CUDPTestDlg::OnStop()
{
// TODO: Add your control notification handler code here
if( socketID != INVALID_SOCKET )
{
#ifdef WIN32
closesocket( socketID ) ;
#else
close( socketID ) ;
#endif
socketID = INVALID_SOCKET ;
}
KillTimer(1);
PrintConsole("Close UDP OK!");
m_StartBtn.ShowWindow(SW_SHOW);
m_StopBtn.ShowWindow(SW_HIDE);
CEdit *thePort = (CEdit*)GetDlgItem(IDC_LOCALPORT);
thePort->EnableWindow(true);
thePort = (CEdit*)GetDlgItem(IDC_PORT);
thePort->EnableWindow(true);
thePort = (CEdit*)GetDlgItem(IDC_LOCALIPADDR);
thePort->EnableWindow(true);
thePort = (CEdit*)GetDlgItem(IDC_IPADDR);
thePort->EnableWindow(true);
}
void CUDPTestDlg::OnOK()
{
// TODO: Add extra validation here
OnStop();
CDialog::OnOK();
}
void CUDPTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
sockaddr_in remote;
int sz = sizeof(sockaddr) ;
char data[512];
int len = recvfrom( socketID, (char *)data, 512,0, (sockaddr *)&remote, &sz ) ;
if ( SOCKET_ERROR != len )
{
data[len]='\0';
PrintConsole("Recv form %s,%d,Data:%s",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port),data);
if(strstr(data,"Yes")!=NULL)
PrintConsole("Recv form %s,%d,Data:%s",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port),data);
if((m_bListenIP == FALSE)||(m_bListenIP && remote.sin_addr.s_addr == m_sin_listen_addr.s_addr))
PrintConsole("Recv form %s,%d,DataLen:%d",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port),len);
if(strstr(data,"Can")!=NULL)
{
strcpy(data,"Yes!I can receive you!");
sendto( socketID, data, strlen(data), 0, (struct sockaddr*)&remote, sizeof(sockaddr_in) ) ;
}
}
CDialog::OnTimer(nIDEvent);
}
void CUDPTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
sockaddr_in remote ;
if( socketID == INVALID_SOCKET )
return ;
UpdateData(true);
//char Buffer[] ="Can you receive me?";
char Buffer[] ="123";
memset( &remote, 0, sizeof(sockaddr_in));
remote.sin_family = AF_INET ;
remote.sin_port = htons(m_LocalPort) ;
remote.sin_addr.s_addr = inet_addr(m_LocalAddress) ;
int len = sendto( socketID, Buffer, strlen(Buffer), 0, (struct sockaddr*)&remote, sizeof(sockaddr_in) ) ;
if ( SOCKET_ERROR == len )
{
PrintConsole("Send OK!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -