title: how get system info in snmpv3 using snmp4j-2.7.0
body: <p>I'm trying to create small app to manage snmp devices and for start i want to get the system info.</p>
  
  <p>I'm using snmp4j version 2.7.0, java 1.8 and the device use snmpv3.
  For start i want to get the data synchronously.
  I try to do it in two ways but i keep getting time out</p>
  
  <p>here is my code</p>
  
  <pre><code>    //Normal request
  public class SynchronouslySnmp 
  {
      protected String PORT="/161";
      protected String PROTOCOL="udp:";
      protected SnmpData snmpData;
      protected Snmp snmp;
  
      public SynchronouslySnmp(SnmpData snmpData)
      {
          this.snmpData=snmpData;
  
      }
  
      public void start()
      {
          createSNMPsession(); 
          addSnmpUser();
          sendSnmp();
      }
  
  
      private void sendSnmp() 
      {
             // send the PDU
          ResponseEvent response;
          try 
          {
              response = snmp.send(getPDU(), getTarget());
              // extract the response PDU (could be null if timed out)
              PDU responsePDU = response.getResponse();
              // extract the address used by the agent to send the response:
              if(responsePDU == null)
              {
                  System.out.println("ERROR: table OID [" + SnmpContants.system + "] time out" );  
              }
              else
              {
                  Address peerAddress = response.getPeerAddress();
                  System.out.println("pdu: "+responsePDU.toString());
                  System.out.println("Address: "+peerAddress.toString());
              }
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
          finally
          {
              try 
              {
                  snmp.close();
              } catch (IOException e) 
              {
                  e.printStackTrace();
              }
          }
      }
  
      protected void createSNMPsession() 
      {
          TransportMapping&lt;? extends Address&gt; transport;
          try 
          {
              transport = new DefaultUdpTransportMapping();
              snmp = new Snmp(transport);
              USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
              SecurityModels.getInstance().addSecurityModel(usm);
              transport.listen();
              //snmp.listen();
          } 
          catch (IOException e) 
          {
              e.printStackTrace();
          }
      }
  
      protected void addSnmpUser()
      {
          // add user to the USM
          snmp.getUSM().addUser(new OctetString(snmpData.getUsmUser()),
                                new UsmUser(new OctetString(snmpData.getUsmUser()),
                                            snmpData.getAuthProtocol(),
                                            new OctetString(snmpData.getAuthPassword()),
                                            snmpData.getPrivProtocol(),
                                            new OctetString(snmpData.getPrivPassword())));
      }
  
      protected Target getTarget()
      {
           // create the target
             UserTarget target = new UserTarget();
             Address targetAddress = GenericAddress.parse(PROTOCOL+snmpData.getIpAddress()+PORT);
             target.setAddress(targetAddress);
             target.setRetries(snmpData.getRetryTimes());
             target.setTimeout(snmpData.getTimeout());
             target.setVersion(snmpData.getSnmpVersion());
             target.setSecurityLevel(snmpData.getSecurityLevel());
             target.setSecurityName(new OctetString(snmpData.getUsmUser()));
             return target;
      }
  
      protected PDU getPDU() 
      {
          // create the PDU
          PDU pdu = new ScopedPDU();
          pdu.add(new VariableBinding(SnmpConstants.system));
          pdu.setType(PDU.GETBULK);
          return pdu;
      }
  }
  </code></pre>
  
  <p>I try to use the treeUtil but got the same error as in the Normal request the follow is my code</p>
  
  <pre><code>    public class SynchronouslySnmpUsingTree extends SynchronouslySnmp
  {
      private OID tableOid;
      private LinkedHashMap&lt;String, List&lt;VariableBinding&gt;&gt; resultMap;
  
      public SynchronouslySnmpUsingTree(SnmpData snmpData)
      {
          super(snmpData);
          resultMap = new LinkedHashMap&lt;String, List&lt;VariableBinding&gt;&gt;();
          tableOid=new OID(".1.3.6.1.2.1.1");
          createSNMPsession(); 
          addSnmpUser();
          sendSnmp();
      }
  
      private void sendSnmp()
      {
          try 
          {
              TreeUtils treeUtils = new TreeUtils(snmp, new PDUFactory()
              {
                  @Override
                  public PDU createPDU(MessageProcessingModel arg0) {
                      return  getPDU();
                  }
  
                  @Override
                  public PDU createPDU(Target arg0) {
  
                      return getPDU();
                  }
              });
              List&lt;TreeEvent&gt; events = treeUtils.getSubtree(getTarget(), tableOid);
              if (events == null || events.size() == 0)
              {
                  System.out.println("Error: Unable to read table...");
                  return;
              }
              for (TreeEvent event : events)
              {
                  if (event != null &amp;&amp; !event.isError()) 
                  {
                      VariableBinding[] varBindings = event.getVariableBindings();
                      if (varBindings != null &amp;&amp; varBindings.length != 0)
                      {
                          for (VariableBinding varBinding : varBindings) 
                          {
                              if (varBinding != null)
                              {
                                  OID oid = varBinding.getOid();
                                  List&lt;VariableBinding&gt; binds = resultMap.get(oid.toString());
                                  if( binds == null)
                                  {
                                      binds = new ArrayList&lt;VariableBinding&gt;();
                                      resultMap.put(oid.toString(), binds);
                                  }
                                  binds.add(varBinding);
                              }
                          }
                      }
                  }
                  else
                  {
                      System.out.println("Error: table OID [" + tableOid + "] " + event.getErrorMessage());
                      continue;
                  }
              }
          }
          catch(Throwable t)
          {
                  System.out.println("Failed operation: getMibTableWithNext");
                  t.printStackTrace();
          }
          finally 
          {
              try
              {
                  snmp.close();
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>main function </p>
  
  <pre><code>public static void main(String[] args){
      SynchronouslySnmp synchronouslySnmp =new SynchronouslySnmp (new SnmpData());
      synchronouslySnmp.start();
      SynchronouslySnmpUsingTree synchronouslySnmpUsingTree = new SynchronouslySnmpUsingTree(new SnmpData());
  }
  </code></pre>
  
  <p>Result of running the program </p>
  
  <pre><code>Error: table OID [1.3.6.1.2.1.1] time out); &lt;---nurmal
  Error: table OID [1.3.6.1.2.1.1] Request time out); &lt;---tree
  </code></pre>
  
  <p>I look in <a href="https://www.snmp4j.org/html/faq.html" rel="nofollow noreferrer">https://www.snmp4j.org/html/faq.html</a></p>
  
  <blockquote>
    <p>Why am I always getting a time-out (response == null) when sending a request?</p>
    
    <blockquote>
      <p>Probably you have forgotten to call the listen() method of the  TransportMapping (once) or the Snmp class before sending the request.</p>
    </blockquote>
  </blockquote>
  
  <p>but i do that in the method createSNMPsession() 
  so what am i doing wrong?
  thanks. :)</p>