C#中开发SNMP程序问题

云眸 2009-08-18 12:59:41
各位高手!大家好!
我现在学习开发SNMP程序,用的是SnmpSharpNet开发包,可从www.snmpsharpnet.com下载,
里面有一个通信例程,每次运行时都是出现请求超时的异常。
也就是UdpTarget类的Request方法出问题了,不知是什么原因?怀疑是不是没有MIB库?望各位大侠帮忙!代码如下


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SnmpSharpNet;
using System.Web;
using System.Net.Sockets;
using System.Net;

namespace MySNMPTest
{
class MyClass
{
static void Main(string[] args)
{
/*
// SNMP community name
OctetString community = new OctetString("public");

// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1 (or 2)
param.Version = SnmpVersion.Ver1;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
//IpAddress agent = new IpAddress("127.0.0.1");

// Construct target*/
AgentParameters param = new AgentParameters();
// Set SNMP protocol version. Only SNMPV1 and SNMPV2 are valid.
param.Version = SnmpVersion.Ver1;
// Set SNMP community name
param.Community.Set("public");
UdpTarget target = new UdpTarget(IPAddress.Parse("127.0.0.1"), 162, 2000, 3);出现问题!

// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName

// Make SNMP request
SnmpV1Packet result = null;
try
{
result = (SnmpV1Packet)target.Request(pdu, param);
}
catch (SnmpException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Retry times is {0}.", target.Retry);
}

// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
// to the VbList
Console.WriteLine("sysDescr({0}) ({1}): {2}",
result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
result.Pdu.VbList[0].Value.ToString());
Console.WriteLine("sysObjectID({0}) ({1}): {2}",
result.Pdu.VbList[1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
result.Pdu.VbList[1].Value.ToString());
Console.WriteLine("sysUpTime({0}) ({1}): {2}",
result.Pdu.VbList[2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
result.Pdu.VbList[2].Value.ToString());
Console.WriteLine("sysContact({0}) ({1}): {2}",
result.Pdu.VbList[3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
result.Pdu.VbList[3].Value.ToString());
Console.WriteLine("sysName({0}) ({1}): {2}",
result.Pdu.VbList[4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
result.Pdu.VbList[4].Value.ToString());
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
target.Close();
Console.ReadLine();
}

}
}
...全文
551 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tlc2000 2009-09-05
  • 打赏
  • 举报
回复
楼主你好,我最近也需要用那个包开发一个网管系统,能否一起交流呀,我的QQ是110066729
chinalong765 2009-08-24
  • 打赏
  • 举报
回复
楼主你好,我也想用Csharp,能否请教你 qq32282202
云眸 2009-08-19
  • 打赏
  • 举报
回复
谢谢1楼这位大侠!问题已经解决!
chinawn 2009-08-19
  • 打赏
  • 举报
回复
这方面没怎么研究,UdpTarget target = new UdpTarget(IPAddress.Parse("127.0.0.1"), 162, 2000, 3)不清楚里面的时间是哪个,我估计是2000,那么改大点;
或者参考下其他request.Timeout = (int)new TimeSpan(0, 0, 60).TotalMilliseconds;request.ReadWriteTimeout="1000";
微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧