[SNMP4J] how should I modify this program using SNMP4J

guogang guo_gang at 126.com
Fri Apr 14 10:50:26 CEST 2006


 
Hello,
I am writing a SNMP trap receiver to do the following:
1.  send a trap
2.  receive it
I have written a programa:
---------------------------------------------------------------------------------------
public class testd implements CommandResponder, PDUFactory { 
 
    private Address address = new UdpAddress("127.0.0.1/162");          
    private OID oid = new OID("1.3.6.1.2.1.1");                  
    private CommunityTarget target = null;   
    private OctetString community = new OctetString("public");        
    public testd()
      {
        target = new CommunityTarget();         
      }
    
    public static void main(String[] args) {
        try {
            testd snmp_ = new testd();
            PDU response = snmp_.send();      
 
        } catch (Exception e) {
           System.out.println("ERROR");
            e.printStackTrace();
        }
    }

    public PDU send() throws IOException {                              
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());      
 
        target.setCommunity(new OctetString("public"));     
        target.setVersion(SnmpConstants.version1);                target.setAddress(address);
        target.setRetries(2);
        target.setTimeout(1000);
                   
        snmp.listen();                                                
 
        PDUv1 request = new PDUv1();        
        request.setType(PDU.V1TRAP);                              
        request.setGenericTrap(PDUv1.COLDSTART);
        request.add(new VariableBinding(SnmpConstants.sysDescr,new OctetString("You System Description")));
        snmp.trap(request,target);    
              
  CommandResponder trapPrinter = new CommandResponder()
    {
        public /*synchronized*/ void processPdu(CommandResponderEvent e) {
        PDU command = e.getPDU();
        if (command != null) 
          {
           System.out.println("success");
            System.out.println(command.toString());
          }
        else 
          {System.out.println("NULL ");}
          }
     };
      snmp.addCommandResponder(trapPrinter);     
               
               
      System.out.println(request);    
 
      snmp.close();
      return request;
      }
    
    public void processPdu(CommandResponderEvent event) {
    }                                              
    
    public PDU createPDU(Target target) {
        PDU aa = null;
        System.out.println("display");
        aa.add(new VariableBinding(SnmpConstants.sysDescr,new OctetString("You System Description")));
        return aa ;                               
    }
}
---------------------------------------------------------------------------------------
 
but it can't receive SNMP traps,.
Could anyone guide me as for how I should do?
Thanks,
Hust.gg
 


More information about the SNMP4J mailing list