www.pudn.com > SyslogTool_Analyse_Final8.6.rar > SyslogToolDlg.cpp
// SyslogToolDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SyslogTool.h"
#include "SyslogToolDlg.h"
#include "tpipv6.h"
#pragma comment(lib,"ws2_32")
#define SYSLOG_NETWORK_EVENT WM_USER+166
#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()
/////////////////////////////////////////////////////////////////////////////
// CSyslogToolDlg dialog
CSyslogToolDlg::CSyslogToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSyslogToolDlg::IDD, pParent),Syslog_Obj(this),IPtype(4)
{
//{{AFX_DATA_INIT(CSyslogToolDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSyslogToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSyslogToolDlg)
DDX_Control(pDX, IDC_IPV4_RADIO, m_IPRadio);
DDX_Control(pDX, IDC_TEST_EDIT, m_TESTEDIT);
DDX_Control(pDX, IDC_PORT_EDIT, m_PORTEDIT);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSyslogToolDlg, CDialog)
//{{AFX_MSG_MAP(CSyslogToolDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
ON_BN_CLICKED(IDC_RESULT_BUTTON, OnResultButton)
ON_BN_CLICKED(IDC_STOP_BUTTON, OnStopButton)
ON_MESSAGE(SYSLOG_NETWORK_EVENT,SyslogOnNetEvent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSyslogToolDlg message handlers
BOOL CSyslogToolDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSyslogToolDlg::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 CSyslogToolDlg::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 CSyslogToolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSyslogToolDlg::SyslogOnNetEvent(WPARAM wParam,LPARAM lParam)
{
int iEvent = WSAGETSELECTEVENT(lParam);
SOCKET pSock = (SOCKET)wParam;
switch(iEvent){
case FD_READ:
SyslogOnReceive(pSock);
break;
default:
break;
}
}
/* Receive the datagram and process */
void CSyslogToolDlg::SyslogOnReceive(SOCKET pSock)
{
int8_t buf[2048];
recv(serverSocket,buf,2048,0);
Syslog_Obj.syslog_process_data(buf);
return;
}
/* Begin to receive the datagram */
void CSyslogToolDlg::OnButton1()
{
// set the port to receive the datagram
char sPORT[10];
int iPORT;
int IPRadio;
m_PORTEDIT.GetWindowText(sPORT,10);
iPORT=atoi(sPORT);
// choose the version of IP and set the socket
IPRadio=GetCheckedRadioButton(IDC_IPV4_RADIO,IDC_IPV6_RADIO);
if((IPRadio==IDC_IPV4_RADIO) || (IPRadio==0))
{
IPtype=4;
}
else if(IPRadio==IDC_IPV6_RADIO)
{
IPtype=6;
}
//set the socket use IPv4
if (IPtype == 4){
serverSocket = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSock;
memset(&addrSock, 0, sizeof(addrSock));
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(iPORT);
addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
if(SOCKET_ERROR==bind(serverSocket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR)))
{
MessageBox("绑定失败!");
exit() ;
}
}else{
//set the socket use IPv6
serverSocket = socket(AF_INET6,SOCK_DGRAM,0);
struct addrinfo hints,*res;
int rc;
memset(&hints,0,sizeof(hints));
char *port = sPORT;
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_DGRAM;
hints.ai_protocol=IPPROTO_UDP;//use UDP
rc=getaddrinfo(NULL,port,&hints,&res);
if(rc!=0)
{
AfxMessageBox("fail");
return;
}
if(SOCKET_ERROR==bind(serverSocket,res->ai_addr,res->ai_addrlen))
{
MessageBox("绑定失败!");
exit() ;
}
}
if(serverSocket == INVALID_SOCKET){
MessageBox("创建套接字失败!");
exit() ;
}
// begin to receive the datagram
m_TESTEDIT.SetWindowText("开始接收数据......");
WSAAsyncSelect(serverSocket,this->m_hWnd, SYSLOG_NETWORK_EVENT,
FD_ACCEPT | FD_CLOSE | FD_READ | FD_WRITE);
}
// The exit button
void CSyslogToolDlg::OnExitButton()
{
closesocket(serverSocket);
WSACleanup();
CDialog::OnCancel();
PostQuitMessage(1);
}
// The button to show the result
void CSyslogToolDlg::OnResultButton()
{
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrTitle="我的打开文件对话框";
fileDlg.m_ofn.lpstrFilter="TEXT File(*.txt)\0*.txt\0ALL FILE(*.*)\0*.*\0\0";
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeRead);
char *pBuf;
DWORD dwFileLen;
dwFileLen=file.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
file.Read(pBuf,dwFileLen);
file.Close();
MessageBox(pBuf);
}
}
//The button to stop receive
void CSyslogToolDlg::OnStopButton()
{
closesocket(serverSocket);
WSACleanup();
m_TESTEDIT.SetWindowText("已停止接受数据!");
}
void CSyslogToolDlg::exit()
{
closesocket(serverSocket);
WSACleanup();
CDialog::OnCancel();
PostQuitMessage(1);
}