SNMP++的深入学习(二)

前端之家收集整理的这篇文章主要介绍了SNMP++的深入学习(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

二、必须经过的阶段Vb类、Pdu类
使用Vb类,主要目的是在处理返回的SNMP应答包时,获得返回的SNMP变量值,有时也需要获得返回的SNMP变量OID。
代码如下:
#include "snmp_pp.h"
#include "oid.h"
#include "vb.h"
#include <iostream.h>
void main()
{
// -------[Ways to construct Vb objects ]-------
// construct a single Vb object 定义一个vb1
Vb vb1;

// construct a Vb object with an Oid object
// this sets the Oid portion of the Vb
Oid d1("1.3.6.1.4.12"); //定义了一个Oid d1,值为1.3.6.1.4.12
Vb vb2(d1);//用d1来为vb2赋值
cout<<"vb2's oid is "<<vb2.get_printable_oid()<<endl;

// construct a Vb object with a dotted string 用一个点分制字符串来定义vb3的oid值
Vb vb3( (Oid) "1.2.3.4.5.6");
cout<<"vb3's oid is "<<vb3.get_printable_oid()<<endl;

// construct an array of ten Vbs
Vb vbs[10];

//------[Ways to set and get the Oid portion of Vb objects ]

// set and get the Oid portion portion的意思是part piece
Oid d2((Oid)"1.2.3.4.5.6");
vb1.set_oid(d2); //用d2的值来定义vb1的oid值,由于现在d2值为1.2.3.4.5.6,所以vb1的oid值也为1.2.3.4.5.6
cout<<"vb1's oid is "<<vb1.get_printable_oid()<<endl;
Oid d3;
vb1.get_oid(d3); //d3取vb1的oid,由于vb1的oid值是用d2的值来定义的,所以d3的值也为1.2.3.4.5.6
cout<<"d3's oid is "<<d3.get_printable()<<endl;
if (d2==d3) cout << "They better be equal!!/n";

Vb ten_vbs[10];
int z;
for (z=0;z<10;z++)
ten_vbs[0].set_oid((Oid)"1.2.3.4.5"); //先对ten_vbs[0]的值作了一个定义

//-------[ ways to set and get values ]

// set & get ints
int x,y;
x=5;
vb1.set_value(x); //将vb1的value值设为x的值,即为5
cout<<"vb1'value is "<<vb1.get_printable_value()<<endl;
vb1.get_value(y);//将vb1的value值设给y
cout<<"y is "<<y<<endl;
if ( x == y) cout << "x equals y/n";
// set and get long ints
long int a,b;
a=100;
cout<<"a is "<<a<<endl;
//-------[ ways to set and get values ]
if ( a == b)cout << "a equals b/n" ;
else cout<<"a is not equals b/n";
// set & get unsigned long ints
unsigned long int c,d;
c = 1000;

vbs[0].set_value( c);//将vbs[0]的value值设为1000
vbs[0].get_value( d);//用vbs[0]值设y的值
if ( c == d) cout << "c equals d/n";

// set a 64 bit counter
Counter64 c64(1000,1001);
vbs[1].set_value( c64);

// get and set an oid as a value
Oid o1,o2;//定义两个Oid o1 o2
o1 = "1.2.3.4.5.6";
vbs[2].set_value( o1);//将vbs[2]的value值设为o1即1.2.3.4.5.6
vbs[2].get_value( o2);//将o2的值设为1.2.3.4.5.6
if ( o1 == o2) cout << "o1 equals o2/n";

// set and get an octet string
unsigned char data[4],outdata[4];
unsigned long len,outlen;
len =4; data[0] = 10; data[1] = 12; data[2] = 12; data[3] = 13;
OctetStr octetstr(data,len);
vbs[3].set_value( octetstr);
vbs[3].get_value( octetstr);

// get & set a string
char beer[80];//定义了一个字符串beer
char good_beer[80]; //定义了一个字符串good_beer
strcpy( beer,"Sierra Nevada Pale Ale");
cout<<"beer is "<<beer<<endl;
vbs[4].set_value( beer); //将beer的值设置给vbs[4]的value
vbs[4].get_value( good_beer); //又将good_beer的值设置为beer的值
printf("Good Beer = %s/n",good_beer);
// get and set an ip an address
IpAddress ipaddress1,ipaddress2;
ipaddress1 = "10.4.8.5";
vbs[5].set_value( ipaddress1);
vbs[5].get_value( ipaddress2);
cout << "ipaddress2 is "<<ipaddress2<<endl;

} // end vb example
运行结果如图7所示

图7Pdu class作为Snmp class的接口,处理SNMP请求源代码如下:#include "snmp_pp.h"#include <iostream.h>#define SYSDECR "1.3.6.1.2.1.1.1.0"// example of how to load a Pdu objectvoid main(){Pdu pdu;// create a Pdu object//创建了一个pduVb vb;// create a Vb object//创建了一个vb对象vb.set_oid( SYSDECR);// set the oid portion of the Vb to System Descriptor//将vb的oid值定义为SYSDECR,也即为1.3.6.1.2.1.1.1.0cout<<"vb's oid is "<<vb.get_printable_oid()<<endl;pdu += vb;// loads the Vb to the Pdu//将vb的值赋给pdu//cout<<pdu.get_vb(vb,0)<<endl;pdu.get_vb(vb,0);cout<<vb.get_printable_oid()<<endl;Pdu my_pdu;// create another Pdu objectVb vbs[5];// create 5 vbspdu.set_vblist( vbs,5);// load all 5 to the pdu}从阻塞或异步请求中得到请求的Pdu后,都需要将Vb卸载下来才能把SMI values取出源程序如下:// example of how to unload a Pdu#include "snmp_pp.h"#include <iostream.h>#define SYSDECR "1.3.6.1.2.1.1.1.0"void main(){Snmp::socket_startup();int status=0;Pdu pdu;// create a Pdu objectVb vb;// create a Vb objectvb.set_oid( SYSDECR);// set the oid portion of the Vb to System Descriptorpdu += vb;// loads the Vb to the Pduchar message[100];// for the system descriptor printable octetSnmp snmp( status);if ( status != SNMP_CLASS_SUCCESS) {cout << "SNMP++ error = "<< snmp.error_msg( status) ;return;}pdu.get_vb( vb,0);// unload the vbvb.get_value( message);// pull the message out of the vbcout << "messages is "<<message<<endl;// print it out}

原文链接:https://www.f2er.com/vb/261723.html

猜你在找的VB相关文章