[SNMP4J] Could anyone please tell me whats wrong with my program...

sanjay A sandy1977r at gmail.com
Thu Jul 19 13:07:13 CEST 2007


Hi all
By using snmp4j packege I have written a trap listener code for snmpV3, but
am not able to catch any trap. In etherial i am getting snmp trap.
Could anyone please tell me whats wrong with my program...


public class V3trapListen {

public V3trapListen() { }

public synchronized void start(String address) throws Exception {
UdpAddress listenAddress = new UdpAddress(address);
ThreadPool threadPool = ThreadPool.create("DispatcherPool", 2);//1
MessageDispatcher mtDispatcher =
new MultiThreadedMessageDispatcher(threadPool,
new MessageDispatcherImpl());//2

// add message processing models
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
mtDispatcher.addMessageProcessingModel(new MPv3());

// add default security models
SecurityProtocols.getInstance().addDefaultProtocols();

Snmp snmp = new Snmp(mtDispatcher);

CommunityTarget target = new CommunityTarget();
if(target != null) {
target.setCommunity(new OctetString("public"));
target.setAddress(listenAddress);
target.setVersion(SnmpConstants.version3);
} else {
System.out.println("Unable to create Target object");
System.exit(-1);
}

CommandResponder trapPrinter = new CommandResponder() {
public void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
System.out.println("\n"+e.getPDU());
}
}
};
if (snmp.addNotificationListener(listenAddress, trapPrinter)) {
snmp.listen();
System.out.println("snmp.addNotificationListener returned TRUE ");
System.out.println("Waiting for [trap] on "+listenAddress.toString());

try {
this.wait();

}
catch(InterruptedException ex) {
System.out.println("Caught InterruptException: "+ex.toString());
}
catch(Exception ex) {
System.out.println("CaughtException: "+ex.toString());
}
} else {
System.out.println("snmp.addNotificationListener returned FALSE");
System.out.println("TestListener closing...");
}
}

regards
sanjay



More information about the SNMP4J mailing list