www.pudn.com > 6manager_server.rar > manager_serverDlg.cpp
// manager_serverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "manager_server.h"
#include "manager_serverDlg.h"
#include "PortSetDlg.h"
#include "snmp_pp.h"
#include "stdlib.h"
#include "string.h"
#define MAX 50
#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()
/////////////////////////////////////////////////////////////////////////////
// CManager_serverDlg dialog
CManager_serverDlg::CManager_serverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CManager_serverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CManager_serverDlg)
m_outinfo = _T("");
m_ininfo = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pSocket=NULL;
m_SocketList.RemoveAll();
}
void CManager_serverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CManager_serverDlg)
DDX_Text(pDX, IDC_SERVER_INFO, m_outinfo);
DDX_Text(pDX, IDC_CLIENT_INFO, m_ininfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CManager_serverDlg, CDialog)
//{{AFX_MSG_MAP(CManager_serverDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PORT_SET, OnPortSet)
ON_BN_CLICKED(IDC_CLEAR_IN, OnClearIn)
ON_BN_CLICKED(IDC_CLEAR_OUT, OnClearOut)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CManager_serverDlg message handlers
BOOL CManager_serverDlg::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
OnPortSet();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CManager_serverDlg::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 CManager_serverDlg::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 CManager_serverDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//=========================进行服务器的端口设置==========================
void CManager_serverDlg::OnPortSet()
{
// TODO: Add your control notification handler code here
CPortSetDlg dlg;
if(dlg.DoModal()==IDOK)
{
m_pSocket=new CListenSocket;
if(m_pSocket->Create(dlg.m_portset))
if(!m_pSocket->Listen())
MessageBox("端口设置错误","服务器程序");
else
MessageBox("端口设置生效","服务器程序");
}
}
//============================接收客户端发来的信息=====================
void CManager_serverDlg::GetSocketMsg(CClientSocket * pSocket)
{
m_ininfo.Empty();
BOOL MsgEnd=FALSE;
CString Msg;
char MsgBuf[100];
int bufsize=100;
Msg.Empty();
do
{
strcpy(MsgBuf,"");
int left=pSocket->Receive(MsgBuf,bufsize);
if(left<100&&left>0)
MsgEnd=TRUE;
MsgBuf[left]=0;
// strcat(pMsg,MsgBuf);
Msg=MsgBuf;
} while(!MsgEnd);
// MessageBox("收到客户端信息","服务器程序");
m_ininfo=Msg;
UpdateData(FALSE);
char pMsg[100];
sprintf(pMsg,"%s",m_ininfo.GetBuffer(100));
// AfxMessageBox(pMsg,MB_OK);
char *ptr;
ptr=pMsg;
while(*ptr!=' ')ptr++;
ptr++;
while(*ptr==' ')ptr++;
if(strstr(pMsg,"getrequest")!=0)
getrequest(ptr);
else if(strstr(pMsg,"getnext")!=0)
getnext(ptr);
else if(strstr(pMsg,"getbulk")!=0)
getbulk(ptr);
else if(strstr(pMsg,"getall")!=0)
getall(ptr);
else
{
//char error_buffer[]="无效的命令";
//m_pSocket->Send(error_buffer,strlen(error_buffer),0);
//AfxMessageBox(error_buffer);
m_outinfo="无效的命令,请查看使用向导后重新输入命令!";
UpdateData(FALSE);
OnOK();
}
// AfxMessageBox(ptr,MB_OK);
}
void CManager_serverDlg::OnClearIn()
{
// TODO: Add your control notification handler code here
m_ininfo.Empty();
UpdateData(FALSE);
}
void CManager_serverDlg::OnClearOut()
{
// TODO: Add your control notification handler code here
m_outinfo.Empty();
UpdateData(FALSE);
}
//==========================返回信息的函数===============================
void CManager_serverDlg::OnOK()
{
// TODO: Add extra validation here
if(!m_SocketList.IsEmpty())
{
UpdateData(TRUE);
POSITION pos;
CClientSocket *pSocket=(CClientSocket *)m_SocketList.GetHead();
for(pos=m_SocketList.GetHeadPosition();pos!=NULL;)
{
pSocket=(CClientSocket *)m_SocketList.GetNext(pos);
pSocket->Send(LPCTSTR(m_outinfo),m_outinfo.GetLength());
}
}
else
MessageBox("没有建立连接","无法发送");
//CDialog::OnOK();
}
void CManager_serverDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_pSocket)
delete m_pSocket;
m_SocketList.RemoveAll();
CDialog::OnCancel();
}
//================================getrequest=========================
void CManager_serverDlg::getrequest(char *ptr)
{
GenAddress address("127.0.0.1");
int status; //返回的SNMP状态
//==========创建SNMP会话,并检查是否成功================
Snmp snmp(status);
if(status!=SNMP_CLASS_SUCCESS)
{
m_outinfo="创建SNMP会话失败";
UpdateData(FALSE);
OnOK();
return;
}
Oid oid(ptr);
if(!oid.valid())
{
m_outinfo="无效的OID";
UpdateData(FALSE);
OnOK();
return;
}
//================设置默认信息================
snmp_version version=version1;
int timeout=50;
int retries=1;
OctetStr community("public");
Vb vb(oid);
Pdu pdu;
pdu+=vb;
CTarget ctarget;
ctarget.set_address(address);
ctarget.set_timeout(timeout);
ctarget.set_retry(retries);
ctarget.set_version(version);
ctarget.set_readcommunity(community);
status=snmp.get(pdu,ctarget);
if(status!=SNMP_CLASS_SUCCESS)
{
if(status == SNMP_CLASS_ERR_STATUS_SET)
status=pdu.get_error_status();
m_outinfo=snmp.error_msg(status);
UpdateData(FALSE);
OnOK();
return;
}
else
{
pdu.get_vb(vb,0);
// m_outinfo="返回的OID=";
// m_outinfo+=vb.get_printable_oid();
// m_outinfo+="返回的value=";
m_outinfo=vb.get_printable_value();
// m_outinfo+="\r\n";
UpdateData(FALSE);
OnOK();
}
}
//==============================getnext==================================
void CManager_serverDlg::getnext(char *ptr)
{
GenAddress address("127.0.0.1");
int status;
Snmp snmp(status);
if(status!=SNMP_CLASS_SUCCESS)
{
m_outinfo="创建SOCKET会话失败";
UpdateData(FALSE);
OnOK();
return;
}
Oid oid(ptr);
if(!oid.valid())
{
m_outinfo="无效的OID,请输入有效的OID";
UpdateData(FALSE);
OnOK();
return;
}
snmp_version version=version1;
int timeout=100;
int retries=1;
OctetStr community("public");
Pdu pdu;
Vb vb(oid);
pdu+=vb;
CTarget ctarget;
ctarget.set_address(address);
ctarget.set_timeout(timeout);
ctarget.set_retry(retries);
ctarget.set_readcommunity(community);
ctarget.set_writecommunity(community);
if((status=snmp.get_next(pdu,ctarget))!=SNMP_CLASS_SUCCESS)
{
if(status==SNMP_CLASS_ERR_STATUS_SET)
status=pdu.get_error_status();
m_outinfo=snmp.error_msg(status);
UpdateData(FALSE);
OnOK();
return;
}
else
{
pdu.get_vb(vb,0);
m_outinfo="返回的OID=";
m_outinfo+=vb.get_printable_oid();
m_outinfo+="\r\n";
m_outinfo+="返回的value=";
m_outinfo+=vb.get_printable_value();
UpdateData(FALSE);
OnOK();
}
}
//==============================getbulk=============================
void CManager_serverDlg::getbulk(char *ptr)
{
GenAddress address("127.0.0.1");
int status;
Snmp snmp(status);
if(status!=SNMP_CLASS_SUCCESS)
{
m_outinfo="创建SOCKET会话失败";
UpdateData(FALSE);
OnOK();
return;
}
Oid oid(ptr);
if(!oid.valid())
{
m_outinfo="无效的OID,请输入有效的OID";
UpdateData(FALSE);
OnOK();
return;
}
snmp_version version=version2c;
int timeout=100;
int retries=1;
OctetStr community("public");
int non_repeaters=0;
int max_repetitions=50;
Pdu pdu;
Vb vb(oid);
pdu+=vb;
CTarget ctarget;
ctarget.set_address(address);
ctarget.set_timeout(timeout);
ctarget.set_retry(retries);
ctarget.set_readcommunity(community);
ctarget.set_writecommunity(community);
ctarget.set_version(version);
if((status=snmp.get_bulk(pdu,ctarget,non_repeaters,max_repetitions))!=SNMP_CLASS_SUCCESS)
{
if(status==SNMP_CLASS_ERR_STATUS_SET)
status=pdu.get_error_status();
m_outinfo=snmp.error_msg(status);
m_outinfo+="\r\n";
UpdateData(FALSE);
OnOK();
return;
}
else
{
m_outinfo.Empty();
for(int i=0;i