[SNMP4J] Socket closed error in Windows 7
Frank Fock
fock at agentpp.com
Tue Aug 17 19:00:01 CEST 2010
Hi,
There is an error in your program: main may return before the thread
you started is finished. It is not related to SNMP4J other than the
new release might be executed with a different speed on your system.
Best regards,
Frank
Am 17.08.2010 um 18:30 schrieb varma datla <varmakdatla at yahoo.com>:
> Hello,
> We are using snmp4j-1.9.3d asynchronously to find devices in a
> network. All is
> working well until we tested it in Windows 7 where it is throwing
> the "Socket
> is closed" exception after a few seconds and stops. Even the latest
> version
> 1.11.1 didn't seemed to work. Luckily we had an older version,
> snmp4j-1.9.1e,
> which is working just fine without any errors.
>
> Here are the details. Could you please suggest any ideas on why this
> is
> happening on Windows 7? or if I'm not using snmp4j appropriately.
>
>
> Environment:
> OS- Windows 7 (Firewall is off)
> SNMP4J- 1.9.3d ( also tested with 1.11.1)
> JVM- 1.6.0_13 (also tested with 1.6.0_20)
> Total addresses- 65025.
> Exception:-
> org.snmp4j.MessageException: Socket is closed
> at org.snmp4j.MessageDispatcherImpl.sendPdu(Unknown Source)
> at org.snmp4j.Snmp.sendMessage(Unknown Source)
> at org.snmp4j.Snmp.send(Unknown Source)
> at org.snmp4j.Snmp.send(Unknown Source)
> at com.some.Snmp4JUtil.send(Snmp4JUtil.java:86)
> at com.some.Snmp4JUtil$3.run(Snmp4JUtil.java:155)
>
>
> Code:
> package com.some;
>
> import java.io.BufferedReader;
> import java.io.BufferedWriter;
> import java.io.FileReader;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.Writer;
> import java.util.LinkedHashSet;
> import java.util.Set;
>
> import org.apache.log4j.Logger;
> import org.snmp4j.CommunityTarget;
> import org.snmp4j.PDU;
> import org.snmp4j.Snmp;
> import org.snmp4j.TimeoutModel;
> import org.snmp4j.TransportMapping;
> import org.snmp4j.event.ResponseEvent;
> import org.snmp4j.event.ResponseListener;
> import org.snmp4j.mp.SnmpConstants;
> import org.snmp4j.smi.OID;
> import org.snmp4j.smi.UdpAddress;
> import org.snmp4j.smi.VariableBinding;
> import org.snmp4j.transport.DefaultUdpTransportMapping;
>
> public class Snmp4JUtil
> {
> private static final Logger LOGGER = Logger.getLogger
> ( Snmp4JUtil.class );
>
> private Snmp snmp;
> private final int [] oid = {1, 3, 6, 1, 4, 1 };
> private final ResponseListener listener;
>
>
> public Snmp4JUtil( final ResponseListener listener )
> {
> this.listener = listener;
>
> try
> {
> final TransportMapping transport = new
> DefaultUdpTransportMapping();
> this.snmp = new Snmp( transport );
> snmp.setTimeoutModel( getTimeoutModel( new int[]{ 500,
> 1000 } ) );
> snmp.listen();
> }
> catch (IOException e)
> {
> LOGGER.error( "Snmp4JUtil(). Error: " + e.getMessage() );
> }
> }
>
> public void close()
> {
> try
> {
> snmp.close();
> }
> catch (IOException e)
> {
> LOGGER.error( "close(). Error: " + e.getMessage() );
> }
> }
>
> public void send( final CommunityTarget target )
> {
>
> final PDU pdu = new PDU();
> pdu.setType( PDU.GETNEXT );
> pdu.add( new VariableBinding( new OID( oid ) ) );
>
> try
> {
> this.snmp.send( pdu, target, null, listener);
> }
> catch (IOException e)
> {
> LOGGER.error( "send( CommunityTarget ) - address:" +
> target.getAddress() + "; error:" + e.getMessage() );
> e.printStackTrace();
> }
> }
>
> private TimeoutModel getTimeoutModel( final int[] timeoutsRetries )
> {
> return new TimeoutModel()
> {
> public long getRequestTimeout( final int totalNumberOfRetries,
> final long targetTimeout )
> {
> long requestTimeout = 0;
>
> for ( int i : timeoutsRetries )
> {
> requestTimeout += i;
> }
>
> return requestTimeout;
> }
>
> public long getRetryTimeout( final int retryCount,
> final int totalNumberOfRetries,
> final long targetTimeout )
> {
> return timeoutsRetries[retryCount];
> }
> };
> }
>
> public static void main( final String args[] )
> throws Exception
> {
> final StringBuilder responses = new StringBuilder();
>
> final ResponseListener listener = new ResponseListener()
> {
> @Override
> public void onResponse( ResponseEvent event )
> {
> //cancel event
> ((Snmp)event.getSource()).cancel(event.getRequest(), this);
>
> // output response here.
> responses.append( "Response: " + event.getResponse() )
> .append( System.getProperty( "line.separator" ) );
> }
> };
>
> final Snmp4JUtil snmpUtil = new Snmp4JUtil( listener );
>
> // A few thousand addresses...just for testing
> final Set<String> addresses = getAddresses( args[0] );
>
> final Thread t = new Thread(){
> public void run()
> {
> for ( final String ipAddress : addresses )
> {
> final CommunityTarget target = new CommunityTarget();
>
> target.setAddress( new UdpAddress( ipAddress + "/
> 161" ) );
> target.setVersion( SnmpConstants.version1 );
> target.setRetries( 1 );
>
> snmpUtil.send( target );
> }
>
> System.out.println("Done requesting..");
>
> // Wait for few more seconds for responses..just for
> testing.
> try
> {
> Thread.sleep( 10000 );
>
> System.out.println( "Writing response to the file.." );
> writeResponse( responses );
>
> snmpUtil.close();
> System.out.println( "Done." );
> }
> catch ( Exception e )
> {
> System.err.println( "Error while sleeping/writing: " +
> e.getMessage() );
> }
> }
> };
>
> t.start();
> }
>
> private static void writeResponse( final StringBuilder sb )
> throws Exception
> {
> final Writer output = new BufferedWriter(new FileWriter(
> "C:/responses.txt" ));
>
> try
> {
> output.write( sb.toString() );
> }
> catch ( Exception e )
> {
> System.err.println( "Error while writing responses: " +
> e.getMessage()
> );
> }
> finally {
> output.close();
> }
> }
>
> private static Set<String> getAddresses( final String fileName )
> throws Exception
> {
> final Set<String> addresses = new LinkedHashSet<String>();
> BufferedReader input = null;
> try
> {
> input = new BufferedReader(new FileReader( fileName ) );
>
> String strLine;
> while ((strLine = input.readLine()) != null)
> {
> if ( strLine.trim().length() > 0 )
> {
> addresses.add( strLine );
> }
> }
>
> }catch ( Exception e )
> {
> System.err.println( "Error while reading addresses: " +
> e.getMessage()
> );
> }
> finally
> {
> if ( input != null )
> {
> input.close();
> }
> }
>
> return addresses;
> }
> }
>
>
> Thank you and I appreciate your help.
>
> Varma
>
>
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
More information about the SNMP4J
mailing list