📄 udpdlg.cpp
字号:
// UDPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDP.h"
#include "UDPDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CUDPDlg dialog
CUDPDlg::CUDPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDPDlg)
m_edit_dst_data = _T("nimengyu");
m_edit_dst_ip = _T("192.168.14.20");
m_edit_recv = _T("");
m_edit_src_port = 5000;
m_edit_static = _T("");
m_edit_dst_port = 4000;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUDPDlg)
DDX_Control(pDX, IDC_EDIT_RECV, m_edit_ctl_recv);
DDX_Control(pDX, IDC_EDIT_STATIC, m_edit_ctl_static);
DDX_Text(pDX, IDC_EDIT_DST_DATA, m_edit_dst_data);
DDX_Text(pDX, IDC_EDIT_DST_IP, m_edit_dst_ip);
DDX_Text(pDX, IDC_EDIT_RECV, m_edit_recv);
DDX_Text(pDX, IDC_EDIT_SRC_PORT, m_edit_src_port);
DDX_Text(pDX, IDC_EDIT_STATIC, m_edit_static);
DDX_Text(pDX, IDC_EDIT_DST_PORT, m_edit_dst_port);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDPDlg, CDialog)
//{{AFX_MSG_MAP(CUDPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SET_SERVER, OnButtonSetServer)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_CLR_STATIC, OnButtonClrStatic)
ON_BN_CLICKED(IDC_BUTTON_CLR_RECV, OnButtonClrRecv)
ON_BN_CLICKED(IDC_BUTTON_CLR_SET, OnButtonClrSet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDPDlg message handlers
BOOL CUDPDlg::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
socket_init();
return TRUE; // return TRUE unless you set the focus to a control
}
void CUDPDlg::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 CUDPDlg::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 CUDPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void *CUDPDlg::ListenThread( void *data )
{
CString ss1,ss2;
char buf[4096];
CUDPDlg *Comm = (CUDPDlg *)data;
int len = sizeof(Comm->client);
while(1) // 一直循环
{
//接收数据
int result = recvfrom( Comm->ListenSocket, buf, sizeof(buf)-1, 0, (sockaddr *)&Comm->client, (socklen_t *)&len);
if ( result > 0 )
{
buf[result] = 0;
ss1.Format("接收到来自 %s 的信息,端口号为 %i \r\n", inet_ntoa(Comm->client.sin_addr), ntohs(Comm->client.sin_port));
ss2.Format(">> %s \r\n", buf);
Comm->m_edit_recv += (ss1 + ss2);
//Comm->update(); //之前一直出错就是因为调用了这句话就出错了
Comm->GetDlgItem(IDC_EDIT_RECV)->SetWindowText(Comm->m_edit_recv);
Comm->m_edit_ctl_recv.LineScroll(Comm->m_edit_ctl_recv.GetLineCount());//滚动条置底
fflush(0);
}
}
}
int CUDPDlg::SetServer()
{
CString ss;
UpdateData(TRUE);
if ( ListenSocket != INVALID_SOCKET )
{
add_static_show("清除原端口\r\n");
closesocket( ListenSocket ); // 如果已经创建、则关闭
ListenSocket = INVALID_SOCKET;
}
if ( ListenSocket == INVALID_SOCKET )
{
ListenSocket = socket(PF_INET, SOCK_DGRAM, 0);
srv.sin_family = PF_INET;
srv.sin_addr.s_addr = htonl( INADDR_ANY ); // 任何地址
srv.sin_port = htons( m_edit_src_port );
if ( bind( ListenSocket, (struct sockaddr *)&srv, sizeof(srv)) != 0 )
{
ss.Format("Error: 绑定%d端口失败\r\n",m_edit_src_port);
add_static_show(ss);
//printf("Error: 绑定失败\n");
fflush(0);
closesocket( ListenSocket );
return false;
}
else
{
ss.Format("绑定%d端口成功\r\n",m_edit_src_port);
add_static_show(ss);
}
int ThreadID; // 线程id
DWORD thread;
//调用createthread创建线程
ThreadID = (int)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(CUDPDlg::ListenThread), (void *)this, 0, &thread);
ThreadID = ThreadID ? 0 : 1; // 如果成功,则返回为0
if(!ThreadID)
{
add_static_show("线程创建成功\r\n");
}
else
{
add_static_show("线程创建失败\r\n");
}
}
//m_edit_ctl_static.LineScroll(m_edit_ctl_static.GetLineCount());//滚动条置底
return 0;
}
void CUDPDlg::OnButtonSetServer()
{
// TODO: Add your control notification handler code here
SetServer();
}
//SendMsg( char *Msg, int Len, char *host, short port )
int CUDPDlg::Send()
{
UpdateData(TRUE);
char host_buf[20];
char *host;
short port;
char Msg_buf[1000];
char *Msg;
int Len;
CString ss;
strcpy(host_buf,m_edit_dst_ip);
host = host_buf;
port = m_edit_dst_port;
strcpy(Msg_buf,m_edit_dst_data);
Msg = Msg_buf;
Len = strlen(m_edit_dst_data);
signed int Sent;
hostent *hostdata;
if ( atoi(host) ) // 是否IP地址为标准形式
{
//u_long ip = inet_addr( host );
//hostdata = gethostbyaddr( (char *)&ip, sizeof(ip), PF_INET );
}
else // 否则则可能是机器名
{
//hostdata = gethostbyname( host );
}
if ( !hostdata )
{
//printf("获得机器名错误\n");
add_static_show("获得机器名错误\r\n");
//fflush(0);
//return false;
}
CString s_ip = m_edit_dst_ip;
sockaddr_in dest; // 发送目标地址
dest.sin_family = PF_INET;
//dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
dest.sin_addr.S_un.S_addr = inet_addr(s_ip);
dest.sin_port = htons( port );
ss.Format("信息已经被发送到主机 %s 端口为 %i \r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port));
add_static_show(ss);
//数据发送
Sent = sendto(ListenSocket, Msg, Len, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
if ( Sent != Len )
{
//printf("错误发送UDP信息\n");
add_static_show("错误发送UDP信息 \r\n");
fflush(0);
return false;
}
return true;
//return 0;
}
void CUDPDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
Send();
}
void CUDPDlg::socket_init()
{
//构造函数
ListenSocket = INVALID_SOCKET; // 开始设置为INVALID_SOCKET
#ifdef _WIN32 // 如果是win32系统
WORD VersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
WSAStartup(VersionRequested, &wsaData); // 启动winsock服务
if ( wsaData.wVersion != VersionRequested )
{
printf("Wrong version or WinSock not loaded\n");
fflush(0);
}
#endif
}
void CUDPDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if ( ListenSocket != INVALID_SOCKET )
closesocket( ListenSocket ); // 如果已经创建、则关闭
#ifdef _WIN32 // 调用WSACleanup
WSACleanup();
#endif
}
void CUDPDlg::add_static_show(CString s)
{
m_edit_static += s;
GetDlgItem(IDC_EDIT_STATIC)->SetWindowText(m_edit_static);
m_edit_ctl_static.LineScroll(m_edit_ctl_static.GetLineCount());//滚动条置底
}
void CUDPDlg::OnButtonClrStatic()
{
// TODO: Add your control notification handler code here
m_edit_static = "";
GetDlgItem(IDC_EDIT_STATIC)->SetWindowText(m_edit_static);
}
void CUDPDlg::OnButtonClrRecv()
{
// TODO: Add your control notification handler code here
m_edit_recv = "";
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_edit_recv);
}
void CUDPDlg::OnButtonClrSet()
{
// TODO: Add your control notification handler code here
m_edit_dst_data = _T("");
m_edit_dst_ip = _T("");
m_edit_recv = _T("");
m_edit_src_port = 0;
m_edit_static = _T("");
m_edit_dst_port = 0;
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -