[SNMP4J] problem receiving traps

null null 9283199b at gmail.com
Fri Nov 7 20:09:36 CET 2008


Hi to all,

I have two small applications:

1) Sender: Application that sends traps continuously using a for loop.
2) Receiver: Application that receives the traps.

The Sender sends 1000 traps, but the Receiver is only receiving about 100
traps.

Am I doing something wrong?
(I'm in windows XP, if that's relevant)

Any help would be very welcomed.

Below is the code for both of the classes  (don't worry, the code is small):

/* *************************************************************************
*/
public class Sender {

    public static void main(String[] args) throws IOException {

        String receiverIPAddress = "10.52.8.223";
        String receiverPort = "162";
        String community = "symmpublic";
        String payload = "hello I'm the payload to send in each trap";

        AbstractTransportMapping transportMapping =
            new DefaultUdpTransportMapping();

        Snmp snmp = new Snmp(transportMapping);

        System.out.println("START traps sending");

        for (int i = 1; i <= 1000; i++) {

            PDUv1 pdu = new PDUv1();
            pdu.setType(PDU.V1TRAP);
            VariableBinding variableBinding = new VariableBinding();
            variableBinding.setVariable(new OctetString(payload));
            pdu.add(variableBinding);

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            Address destinationAddress = GenericAddress.parse(
                receiverIPAddress + "/" + receiverPort);
            target.setAddress(destinationAddress);
            target.setVersion(SnmpConstants.version1);

            snmp.send(pdu, target);
        }

        snmp.close();

        System.out.println("END traps sending");
    }
}

/* *************************************************************************
*/

public class Receiver {

    private static int receivedCount = 0;

    public static void main(String[] args) throws Exception {

        String receiverIPAddress = "10.52.8.223";
        String receiverPort = "162";

        Address address = GenericAddress.parse(
                receiverIPAddress + "/" + receiverPort);

        AbstractTransportMapping transport = null;
        if (address instanceof TcpAddress) {
            transport = new DefaultTcpTransportMapping((TcpAddress)
address);
        } else {
            transport = new DefaultUdpTransportMapping((UdpAddress)
address);
        }

        MessageDispatcher dispatcher = new MessageDispatcherImpl();
        dispatcher.addMessageProcessingModel(new MPv1());
        SecurityProtocols.getInstance().addDefaultProtocols();
        Snmp snmp = new Snmp(dispatcher, transport);

        CommandResponder commandResponder = new CommandResponder() {
            public void processPdu(CommandResponderEvent arg0) {
                receivedCount++;

                try {
                    // Simulates time consumed by pdu dispatching.
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        snmp.addCommandResponder(commandResponder);
        System.out.println("START traps listening.");
        transport.listen();

        // listen for 1 minute, then exit.
        Thread.sleep(60000);
        snmp.close();
        transport.close();

        System.out.println("END traps listening.");
        System.out.println("received PDUs: " + receivedCount);
    }
}



More information about the SNMP4J mailing list