关于#java#的问题:java对接华为snmp接口

麻烦帮忙看看下面这个代码有什么问题?


public class SnmpTest01 {

    private Snmp snmp = null;

    private Address targetAddress = null;

    public void initComm() throws IOException {
        // 设置Agent方的IP和端口
        targetAddress = GenericAddress.parse("udp:172.18.1.14/161");
        TransportMapping transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
    }

    public ResponseEvent sendPDU(PDU pdu) throws IOException {
        // 设置 target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        // 通信不成功时的重试次数
        target.setRetries(2);
        // 超时时间
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version2c);
        // 向Agent发送PDU,并返回Response
        return snmp.send(pdu, target);
    }

    public void setPDU() throws IOException {
        // set PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST")));
        pdu.setType(PDU.SET);
        sendPDU(pdu);
    }

    public void getPDU() throws IOException {
        // get PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));
        pdu.setType(PDU.GET);
        readResponse(sendPDU(pdu));


        PDU pdu1 = new PDU();
        pdu1.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));
        pdu1.setType(PDU.GET);
        readResponse(sendPDU(pdu1));
    }

    public void readResponse(ResponseEvent respEvnt) {
        // 解析Response
        if (respEvnt != null && respEvnt.getResponse() != null) {
            Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getResponse().getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                System.out.println(recVB.getOid() + " : " + recVB.getVariable());
            }
        }
    }

    public static void main(String[] args) {
        try {
            SnmpTest01 util = new SnmpTest01();
            util.initComm();
            util.setPDU();
            util.getPDU();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你测试有什么问题