[SNMP4J] trap receive
huang hongxing
huanghongxing at tsinghua.org.cn
Wed Aug 24 11:07:51 CEST 2005
Hello,
I have one trap receive question. I have seen in the class snmpRequest but I am not able to find problem. here two code examples,in wsad5,the console reports for the two examples are the same :
java.lang.NoClassDefFoundError: java/net/SocketAddress
at Snmptraptest.<init>(Snmptraptest.java:20)
at Snmptraptest.main(Snmptraptest.java:58)
Exception in thread "main"
example 1:
import java.io.IOException;
import org.snmp4j.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.util.*;
import org.snmp4j.event.*;
public class Snmptraptest {
public Snmptraptest() {
int numDispatcherThreads = 2;
try {
TransportMapping transport =
new DefaultUdpTransportMapping(new UdpAddress("udp:127.0.0.1/162"));
CommandResponder trapPrinter = new CommandResponder() {
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
System.out.println(command.toString());
}
}
};
ThreadPool threadPool =
ThreadPool.create("DispatcherPool", numDispatcherThreads);
MessageDispatcher mtDispatcher =
new MultiThreadedMessageDispatcher(
threadPool,
new MessageDispatcherImpl());
mtDispatcher.addMessageProcessingModel(new MPv1());
Snmp snmp = new Snmp(mtDispatcher, transport);
snmp.addCommandResponder(trapPrinter);
transport.listen();
try {
this.wait();
} catch (InterruptedException ex) {
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
System.exit(0);
}
}
public static void main(String[] args) {
Snmptraptest snmptrap = new Snmptraptest();
}
}
example 2:
public class Snmptrap implements CommandResponder {
public Snmptrap() {
}
public void init() {
try {
TransportMapping transport =
new DefaultUdpTransportMapping(new UdpAddress("0.0.0.0/162"));
Snmp snmp = new Snmp(transport);
snmp.addCommandResponder(this);
transport.listen();
System.out.println("Listening on " + "localhost/162");
} catch (IOException ex) {
System.err.println(
"Error while trying to send request: " + ex.getMessage());
ex.printStackTrace();
}
try {
this.wait();
} catch (InterruptedException ex) {
}
}
/**
* processPdu
*
* @param e CommandResponderEvent
*/
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
System.out.println(command.toString());
}
}
public static void main(String[] args) {
Snmptrap snmptrap = new Snmptrap();
snmptrap.init();
}
}
Best regards,
huang hongxing
huanghongxing at tsinghua.org.cn
2005-08-24
More information about the SNMP4J
mailing list