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();
}

}
}
...全文
575 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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";

111,092

社区成员

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

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

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