[SNMP4J] Exception thrown in DefaultTcpTransportMapping
Rodger Lucas
rlucas at tazznetworks.com
Fri Mar 23 16:38:51 CET 2007
Hi:
I came across this while running junit testcases where I was
initializing and unitializing SNMP4J objects. The following code causes
an exception when using DefaultTcpTransportMapping but not when using
DefaultUdpTransportMapping.
Consider the following code below. There are two methods which create an
org.snmp4j.Snmp object sequentially with the appropriate cleanup/close
in between. One method uses DefaultUdpTransportMapping and the other
DefaultTcpTransportMapping. I have commented where the Exception is
thrown.
When running the test program we see the following in the console:
--------BEGIN CONSOLE OUTPUT --------------
Start testUdp
First startup
First sleep
First shutdown
Second startup
Second sleep
Second shutdown
End testUdp
Start testTcp
Second startup
First sleep
First shutdown
Second startup
Exception in tcp test: java.net.BindException - Address already in use
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind(Native Method)
at
sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119
)
at
sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at
sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:52)
at
org.snmp4j.transport.DefaultTcpTransportMapping$ServerThread.<init>(Unkn
own Source)
at
org.snmp4j.transport.DefaultTcpTransportMapping.listen(Unknown Source)
at com.tazz.proto.TestTwo.testTcp(TestTwo.java:126)
at com.tazz.proto.TestTwo.main(TestTwo.java:159)
End testTcp
--------END CONSOLE OUTPUT --------------
I am using SNMP4J.jar version 1.8.1
Java VM is:
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_07-b03, mixed mode)
Hardware / OS: SunOS psd0a01 5.10 Generic_118855-15 i86pc i386 i86pc
Interestingly when this same program is run on Windows XP the exception
is NOT thrown.
Here is the Code
---------------BEGIN CODE --------------------------
package com.tazz.proto;
import java.lang.Exception;
import java.lang.String;
import java.lang.System;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDUv1;
import org.snmp4j.PDU;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.Snmp;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.transport.DefaultTcpTransportMapping;
public class TestTwo
{
private Snmp _snmp = null;
private DefaultUdpTransportMapping _udpTransport = null;
private DefaultTcpTransportMapping _tcpTransport = null;
private SnmpTrapListener _listener = null;
public TestTwo()
{
}
public void testUdp() {
System.out.println( "Start testUdp" );
int port = 3333;
try {
System.out.println( " First startup" );
_listener = new SnmpTrapListener();
_udpTransport = new DefaultUdpTransportMapping( new UdpAddress(
port ) );
_snmp = new Snmp( _udpTransport );
_udpTransport.listen();
_snmp.addCommandResponder( _listener ); // for notifications
System.out.println( " First sleep" );
Thread.sleep( 3000 ); // sleep for fun
System.out.println( " First shutdown" );
_snmp.removeCommandResponder( _listener );
_udpTransport.close();
_snmp.close();
_snmp = null;
_udpTransport = null;
_listener = null;
// Lets do it again ....
System.out.println( " Second startup" );
_listener = new SnmpTrapListener();
_udpTransport = new DefaultUdpTransportMapping( new UdpAddress(
port ) );
_snmp = new Snmp( _udpTransport );
_udpTransport.listen();
_snmp.addCommandResponder( _listener ); // for notifications
System.out.println( " Second sleep" );
Thread.sleep( 3000 ); // sleep for fun
System.out.println( " Second shutdown" );
_snmp.removeCommandResponder( _listener );
_udpTransport.close();
_snmp.close();
_snmp = null;
_udpTransport = null;
_listener = null;
}
catch ( Exception ex ) {
System.out.println( "Exception in UDP test: " +
ex.getClass().getName() + " - " + ex.getMessage() );
ex.printStackTrace();
}
System.out.println( "End testUdp" );
}
public void testTcp() {
System.out.println( "\nStart testTcp" );
int port = 3333;
try {
System.out.println( " Second startup" );
_listener = new SnmpTrapListener();
_tcpTransport = new DefaultTcpTransportMapping( new TcpAddress(
port ) );
_snmp = new Snmp( _udpTransport );
_tcpTransport.listen();
_snmp.addCommandResponder( _listener ); // for notifications
System.out.println( " First sleep" );
Thread.sleep( 3000 ); // sleep for fun
System.out.println( " First shutdown" );
_snmp.removeCommandResponder( _listener );
_tcpTransport.close();
_snmp.removeTransportMapping( _tcpTransport );
_snmp.close();
_snmp = null;
_tcpTransport = null;
_listener = null;
// Lets do it again ....
System.out.println( " Second startup" );
_listener = new SnmpTrapListener();
_tcpTransport = new DefaultTcpTransportMapping( new TcpAddress(
port ) );
_snmp = new Snmp( _tcpTransport );
_tcpTransport.listen(); // <<< EXCEPTION HERE
_snmp.addCommandResponder( _listener ); // for notifications
System.out.println( " Second sleep" );
Thread.sleep( 3000 ); // sleep for fun
System.out.println( " Second shutdown" );
_snmp.removeCommandResponder( _listener );
_tcpTransport.close();
_snmp.removeTransportMapping( _tcpTransport );
_snmp.close();
_snmp = null;
_tcpTransport = null;
_listener = null;
}
catch ( Exception ex ) {
System.out.println( "Exception in tcp test: " +
ex.getClass().getName() + " - " + ex.getMessage() );
ex.printStackTrace();
}
System.out.println( "End testTcp" );
}
public static void main( String[] argv )
{
TestTwo test = new TestTwo();
test.testUdp();
test.testTcp();
}
public class SnmpTrapListener implements CommandResponder
{
public void processPdu(CommandResponderEvent event)
{
String oid = "";
PDU command = event.getPDU();
boolean isV1 = false;
try {
if (command != null) {
System.out.print( "Received: " + command.toString() + "
from " + event.getPeerAddress().toString() );
}
}
catch ( Exception ex ) {
System.out.print( "Received exception " +
ex.getClass().getName() + " occurred: " + ex.getMessage() );
ex.printStackTrace();
}
}
}
}
---------------END CODE -------------------------
Is there something amiss ?
Regards,
Rodger Lucas
TAZZ Networks
Glasgow
Scotland, United Kingdom
Voice: +44 (0)141 222 2126
Email: rlucas at tazznetworks.com <mailto:rlucas at tazznetworks.com>
More information about the SNMP4J
mailing list