Writing SNMP Manager using SNMP4J


I am writing a SNMP Manager that is supposed to do the following:
(1) Send snmp commands to snmp-agents, and handle thier response.
(2) Receive snmp traps from snmp-agents.

I am using SNMP4J software package for writing the snmp manager.

I would like to use a SINGLE Snmp object (SNMP4J object) , both
for sending a snmp command and for receiving snmp traps.

So far, I have written two separate programs:

*** Program 1:
** The first program only listens for Snmp trap-PDUs.
** Its main code is as follows:

// configure Snmp object
UdpAddress address = new UdpAddress("");
TransportMapping transport = new DefaultUdpTransportMapping(address);
Snmp snmp = new Snmp(transport);

// handle received traps here
CommandResponder trapPrinter = new CommandResponder() {
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPdu();
if (command != null) {

*** Program 2:
** The second program sends GET PDUs, and its main code is as follows:

// configure Snmp object
UdpAddress targetAddress = new UdpAddress("");
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setTimeout(10000); // was 1000 !!!
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());

// prepare the PDU for sending
PDU command = new PDU();
command.add(new VariableBinding(new

// now send the PDU
ResponseEvent responseEvent = snmp.send(pdu, target);
if (responseEvent != null)
// response has arrived. handle it
System.out.println("null response received...");


I would like to unite these two programs into one, however, I cannot
seem to
properly allocate one Snmp object to handle both the sending and
receiving of PDUs.

I run two threads: thread-1 that does the listening for snmp traps,
and thread-2 which in charge of sending get/set PDUs.

I allocate the Snmp object in one place as follows:

TransportMapping transport = new DefaultUdpTransportMapping(new
Snmp snmp = new Snmp(transport);

and I transfer it to both threads to be used there.
However, thread-2 sometimes fails to send PDUs using snmp.send(...);

What am I doing wrong?
Can anyone guide me as for how I should allocate the Snmp object so
it is good for both sending PDUs, and for receving traps?