[SNMP4J] Listen and send commands from the same port

Nikodem DobrzaƄski nexpron at gmail.com
Wed Mar 5 13:29:33 CET 2014


Hi everyone

I tried create a snmp object that listen incoming messages/responses on 162
UDP port number and is able to send getnext command  from port 162 UDP only
to 161 (others ports are not available). Object listen traps without
problems but there's no command response. Message model processing is
SNMPv1.
I usually get error org.snmp4j.transport.UnsupportedAddressClassException:
Unsupported address class (transport mapping): org.snmp4j.smi.UdpAddress

Pieces of used code below

private TransportMapping transport = null;
private MessageDispatcher mtDispatcher = null;

// for listen
public synchronized void listen(TransportIpAddress address) throws
IOException
{
InetAddress managerIpAddress = InetAddress.getLocalHost();
UdpAddress managerUdpAddress = new UdpAddress(managerIpAddress, 162);
this.transport = new DefaultUdpTransportMapping(managerUdpAddress);
ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
this.mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new
MessageDispatcherImpl());
this.mtDispatcher.addMessageProcessingModel(new MPv1());
SecurityProtocols.getInstance().addDefaultProtocols();
snmp = new Snmp(mtDispatcher, transport);

CommandResponder trapPrinter = new CommandResponder()
{
@Override
        public synchronized void processPdu(CommandResponderEvent
cmdRespEvent) {
                PDU pdu = cmdRespEvent.getPDU();
...
}
};


if(snmp.addNotificationListener(listenResponseAddress, trapPrinter))
        {
            snmp.listen();
...
}

// for send
public void SendGetNext(){
Address add = GenericAddress.parse("udp" + ":" + "196.31.82.19" + "/" +
161);
CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString("ABC"));
        comtarget.setVersion(SnmpConstants.version1);
        comtarget.setAddress(add);
        comtarget.setRetries(2);
        comtarget.setTimeout(1000);

OID rootOid = new OID("1.3.6.1.4.1.191"); // example
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(rootOid));
        pdu.setType(PDU.GETNEXT);

try{
this.snmp.send(pdu, comtarget);
}
catch(IOException ex){
            System.out.println("Error" + ex.toString());
}



More information about the SNMP4J mailing list