[SNMP4J] DefaultTcpTransportMapping: socket disconnect causes timeouts for all

Ladd ladd at codemettle.com
Thu Jun 19 00:18:37 CEST 2014


Frank Fock <fock at ...> writes:

> 
> 
> Am 05.06.2014 23:03, schrieb Ladd:
> >
> > I see.  Was MIB Explorer using a single session instance with a single
> > TableUtils instance calling the synchronous getTable() to do its queries?
> >
> > Thanks!!
> >
> > - Ladd
> >
> MIB Explorer uses a single Snmp instance but no TableUtils instance (for 
> my test).
> In any case, an asynchronous request model is used (although it should 
> not make
> any difference).
> 
> Best regards,
> Frank
> 

Hi again Frank,

I thought I had a workaround for this TCP timeout problem but it appears
not.  So I wrote a very small, standalone example that demonstrates this
problem.  I'll paste it here, but if there's a better method to show you the
source, please let me know.

This little test polls for the sysDescr of two agents running on two
different machines from the poller.  In all my tests on Windows and Linux
with Java 1.6 and 1.7, killing one of the polled agents affects (causes
timeouts for) the other.  But only with TCP.  With UDP, communication the
still-running agent is unaffected.

It's probably something simple so I hope you can tell me what's wrong.

Change the USE_TCP, IP_1, and IP_2 variables as needed.

Thanks Frank!

/**
 * 
 */
package snmp4j.over.tcp.test;

import java.util.Calendar;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 * @author ladd
 *
 */
public class TestSnmp4jOverTcp {

	static Snmp session;
	static boolean USE_TCP = true;
	static String IP_1 = "192.168.1.92";
	static String IP_2 = "192.168.1.102";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {

			if ( USE_TCP ) {
				session = new Snmp( new DefaultTcpTransportMapping() );
			} else {
				session = new Snmp( new DefaultUdpTransportMapping() );
			}
			
			session.listen();

		} catch (Exception e) {
			System.err.println( "Could not start SNMP transport: " + e.toString() );
		}

		final CommunityTarget t1 = createTarget( IP_1, 161 );
		final CommunityTarget t2 = createTarget( IP_2, 161 );

		kickOffPollThread(t1);
		try { Thread.sleep( 500 ); } catch (InterruptedException e) { }
		kickOffPollThread(t2); 
	}

	static void kickOffPollThread( final CommunityTarget target ) {
		
		new Thread(
				new Runnable() { 
					public void run() {

						long startTime;
						long endTime;
						long duration;
						PDU pdu = new PDU();
						pdu.add( new VariableBinding( new OID( "1.3.6.1.2.1.1.1.0" ) ) ); //
sysDescr
						
						while( true ) {
							try {
								
								startTime = System.currentTimeMillis();
								ResponseEvent responseEvt = session.send( pdu, target );
								endTime = System.currentTimeMillis();
								duration = endTime - startTime;
								
								PDU responsePDU = responseEvt.getResponse();
								if ( responsePDU != null ) {
									System.out.println( getTime() + " - " + target.getAddress() +
											" " + responsePDU.getVariableBindings() + " (" + duration + "ms)" );
								} else {
									System.out.println( getTime() + " - " + target.getAddress() +
											" " + "response PDU is null" + " (" + duration + "ms)" );
								}
								Thread.sleep( 1000 );
							} catch (Exception e) {
								System.err.println( "Send error: " + e );
							}
						}
					} 
				}
		).start();
	}
	
	static CommunityTarget createTarget( String ipAddress, int port ) {
        
        CommunityTarget target = new CommunityTarget();
        target.setCommunity( new OctetString( "public" ) );
        if ( USE_TCP ) {
        	target.setAddress( new TcpAddress( ipAddress + "/" + port ) );
        } else {
        	target.setAddress( new UdpAddress( ipAddress + "/" + port ) );
        }
        target.setRetries( 0 );
        target.setTimeout( 5000 );
        target.setVersion( SnmpConstants.version1 );

        return target;
	}
	
	static String getTime() {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());

        final long hr = c.get(Calendar.HOUR_OF_DAY);
        final long min = c.get(Calendar.MINUTE);
        final long sec = c.get(Calendar.SECOND);
        final long ms = c.get(Calendar.MILLISECOND);
        return String.format("%02d:%02d:%02d.%03d", hr, min, sec, ms);
	}
}






More information about the SNMP4J mailing list