Windows 11环境下无法接收SNMP Trap问题求助
Windows 11 下 SnmpSharpNet 无法接收 SNMP Trap 问题
我在 .NET 7 与 C# 10 环境中使用 SnmpSharpNet 库接收网络中的所有 SNMP Trap,代码基本来自官方文档,在 Ubuntu LTS 系统上无需修改即可正常运行,但在 Windows 11 系统中无法捕获 SNMP Trap,导致无法本机调试程序。
首次尝试未修改 Windows 11 任何设置,查阅资料得知 Windows 11 默认不支持 SNMP,已通过「设置>应用>可选功能>添加可选功能」安装 SNMP 服务,但代码仍无法接收 Trap。
使用的代码如下:
using SnmpSharpNet; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162); EndPoint ep = (EndPoint)ipep; socket.Bind(ep); // Disable timeout processing. Just block until packet is received socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0); bool run = true; int inlen = -1; while (run) { byte[] indata = new byte[16 * 1024]; // 16KB receive buffer int inlen = 0; IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0); EndPoint inep = (EndPoint)peer; try { inlen = socket.ReceiveFrom(indata, ref inep); } catch (Exception ex) { inlen = -1; } if (inlen > 0) { // Check protocol version int int ver = SnmpPacket.GetProtocolVersion(indata, inlen); if (ver == (int)SnmpVersion.Ver1) { SnmpV1TrapPacket pkt = new SnmpV1TrapPacket(); pkt.decode(indata, inlen); //Do things... } else { SnmpV2Packet pkt = new SnmpV2Packet(); pkt.decode(indata, inlen); //Do things... } } else { if (inlen == 0) Trace.WriteLine("Zero length packet received."); } }
内容的提问来源于stack exchange,提问作者Majico
相关产品推荐
相关产品推荐

