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?



Relevant Pages

  • Re: How do I get the port on a switch that a PC is connected to.
    ... With SNMP there are two distinct concepts. ... "the command tcpConnLocalPort" - you ... If the switch maintains this information, you are in luck and you can ... The advantage of this method is you can directly get the port of the ...
  • Re: SNMP in rails
    ... net/snmp library for ruby and get and set methods of ... I'm guessing that the first snmpset command is used to reset port status ... The second command snmpget is used to read port reset status. ... I am using snmp library of ruby but need to pass the snmpset php ...
  • Re: SNMP with PB 4.2 - test sample not being built ?
    ... Because if I add it there my platform nk.bin will not be ... >Basically what you're missing is snmp_snmptestmib showing ... >other SNMP components? ... Did also a command like ...
  • Re: Doubt regarding adding a row in table using SNMPv2
    ... without the intervention of a manager. ... It seems like your understanding of what an SNMP agent does ... If an "SNMP agent" is doing things that are not in response ...
  • Re: Question regarding SNMP GET Response PDU
    ... This all depends on which version of the SNMP protocol you're using. ... If you are using an SNMP toolkit which has a smart manager API, ... which he will receive a succesful response containing these two values. ... It can be either noSuchInstance or noSuchObject - refer to ...