[SNMP4J] SNMP code that does not work with TCP

Anmol Sinha anmolinpune at yahoo.co.in
Thu Sep 22 13:32:48 CEST 2005


Hello Frank,
 
Attached is the java code written for SNMP using TCP.
I am using the latest SNMP4J.JAR (v1.5.1).
The given code works fine for UDP.
 

import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
 
public class SNMPV3Test1
{     
    public void  RunnableSender()
    {
     try
     {
        Address targetAddress = GenericAddress.parse("tcp:193.168.1.79/4700");
        TransportMapping transport = new DefaultTcpTransportMapping();
        Snmp snmp = new Snmp(transport);
        MPv3 mpv3 =
            (MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3);
        USM usm = new USM(SecurityProtocols.getInstance(),
                          new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();
                
        snmp.getUSM().addUser(new OctetString("MD5DES"),
              new UsmUser(new OctetString("MD5DES"),
                          AuthMD5.ID,
                          new OctetString("MD5DESUserAuthPassword"),
                          PrivDES.ID,
                          new OctetString("MD5DESUserPrivPassword")));
                         
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(5000);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
        target.setSecurityName(new OctetString("MD5DES"));
  
        
        PDU pdu = new ScopedPDU();
        pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.16.1.2.1.3.1.6.112.117.98.108.105.99")));
        pdu.setType(PDU.GET);
  
        // send the PDU
        ResponseEvent response = snmp.send(pdu, target);
  
        PDU responsePDU = response.getResponse();
        System.out.println("Response is..." + responsePDU.toString());
        
        Address peerAddress = response.getPeerAddress();
        System.out.println("Address is..." + peerAddress.toString());
     }
     catch(Exception exp){
      exp.printStackTrace();
     }
    }
  public static void main(String[] args) throws InterruptedException{
   SNMPV3Test1 snmpRequest = new SNMPV3Test1();
    snmpRequest.RunnableSender();
  }
}
 
 
The stack trace is....

java.lang.NullPointerException

at SNMPV3Test1.RunnableSender(SNMPV3Test1.java:63)

at SNMPV3Test1.main(SNMPV3Test1.java:74)

 
 
If I replace the TCP method with UDP i.e.
        Address targetAddress = GenericAddress.parse("udp:193.168.1.79/4700");
        TransportMapping transport = new DefaultUdpTransportMapping();

The code works fine and gives the expected result.

 
Thanks & Regards,
Anmol
 
 
 
 
 
 
 

Frank Fock <fock at agentpp.com> wrote:
Hello Anmol,

Could you please post the stack trace of the NullPointerException you get
and the SNMP4J version you are using?

Thanks in advance,
Frank

Anmol Sinha wrote:

>Hello All,
> 
>I have Sucessfully written and tested the SNMP code for UDP protocol for all versions i.e. 1, 2 & 3.
>I want to have the same code to support TCP protocol also. I just replaced statement...
>DefaultUdpTransportMapping dutm = new DefaultUdpTransportMapping();
>with
>DefaultTcpTransportMapping dutm = new DefaultTcpTransportMapping();
> 
>But on execution i get Null Pointer Exception. The code works fine for UDP.
>Can someone help me, so that I know if I have missed out something or some configuration changes.
> 
>Thanks,
>Anmol
>
> 
>---------------------------------
> Yahoo! India Matrimony: Find your partner now.
>_______________________________________________
>SNMP4J mailing list
>SNMP4J at agentpp.org
>http://lists.agentpp.org/mailman/listinfo/snmp4j
>
>


		
---------------------------------
 Yahoo! India Matrimony: Find your partner now.


More information about the SNMP4J mailing list