zoukankan      html  css  js  c++  java
  • snmp4j 异步获取节点信息

    1. 主要代码如下:

    public class ResponseListenerTest {
        public static void main(String[] args) throws IOException, InterruptedException {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
            snmp.listen();
            
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));
            target.setAddress(GenericAddress.parse("udp:192.168.100.61/161"));
            target.setRetries(1);
            target.setTimeout(2000);
            target.setVersion(SnmpConstants.version1);
            
            
            PDU pdu = new PDU();
            pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0")));
            pdu.setType(PDU.GET);
            
            ResponseListener listener = new ResponseListener() {
                @Override
                public void onResponse(ResponseEvent event) {
                    PDU resp = event.getResponse();
                    VariableBinding vb = resp.get(0);
                    System.out.println(vb.getOid().toString() + "^^^^" + vb.getVariable());
                }
            };
            CountDownLatch latch = new CountDownLatch(1);
            snmp.get(pdu, target, null, listener);
            latch.await(2, TimeUnit.SECONDS);
        }
    }

    2. 运行结果如下:

    3. 在上面的例子中,也可以使用线程的方式处理:

    但是需要注意的是:sleep的时间要小于 setTimeout 的时间,否则会出现异常。

     4. 其中:

      target.setTimeout(2000); // 意思为:当发送请求后 2秒钟没有返回响应信息,表示已经超时了。
      target.setRetries(1);   // 意思为:当上面的逻辑超时后,再次发送请求的次数,为1次。
     
  • 相关阅读:
    J2ME 游戏开发之GameCanvas的使用
    J2ME游戏开发之字符串的绘制
    JS数组操作
    什么是LBS?地理位置服务
    js中的this怎么理解
    相机参数
    boost 移植到ARM EP9315
    armlinuxgcc 安装和配置
    小算法 : 水仙花数
    C语言标准库 文件操作
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7218248.html
Copyright © 2011-2022 走看看