[SNMP4J] Socket closed error in Windows 7

varma datla varmakdatla at yahoo.com
Thu Aug 26 15:54:14 CEST 2010


Hello Frank,
After some more testing, we have decided to downgrade to snmp4j-1.9.1e as it is 
working fine..for now. Not sure yet if it has to do with the fastness of the 
latest version or with timing issues.
I have also tested by sending a datagram packet for a large number of addresses 
without using snmp4j to see if this is happening with the JRE 1.6.* (32 and 64 
bit) in Windows 7. I saw the same 'Socket closed' errors after a few seconds. 
Did the same test using Ruby on the same machine and is successful with no 
errors at all.

So, not sure what is happening or if I'm doing something wrong. I appreciate any 
suggestions.

Thanks,
Varma



________________________________
From: varma datla <varmakdatla at yahoo.com>
To: Frank Fock <fock at agentpp.com>
Cc: "snmp4j at agentpp.org" <snmp4j at agentpp.org>
Sent: Tue, August 17, 2010 2:07:47 PM
Subject: Re: [SNMP4J] Socket closed error in Windows 7


Hello Frank,
I apologize for that silly mistake and thanks for pointing it out. I've added a 
CountDownLatch so that the main() wouldn't exit unit all requests are being 
responded.
Actually, we had this running in Tomcat..but just want to have a standalone 
program to reproduce the problem quicker. Please let me know if you want me to 
run any further tests on Windows 7.

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 java.util.concurrent.CountDownLatch;

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 )
   {  
      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();
      
      // A few thousand addresses...just for testing
      final Set<String> addresses = getAddresses( args[0] );
      
      final CountDownLatch latch = new CountDownLatch( addresses.size() );
      
      final ResponseListener listener = new ResponseListener()
      {  
         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" ) );
            
            latch.countDown();
            System.out.println("countDown:" + latch.getCount());
         }
      };      

      final Snmp4JUtil snmpUtil = new Snmp4JUtil( listener );
      
      
      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 );
           }
           catch (  Exception e )
           {
              System.err.println( "Error while sleeping/writing: " + 
e.getMessage() );
           }
        }
      };
      
      t.start();
      
      System.out.println("Started running the thread and waiting for 
countdown.");
      
      latch.await();
      
      System.out.println("Finished waiting..closing snmp.");
      
      snmpUtil.close();
      System.out.println( "Done." );       
   }
   
   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;
   }
}

Thanks,
Varma



________________________________
From: Frank Fock <fock at agentpp.com>
To: varma datla <varmakdatla at yahoo.com>
Cc: "snmp4j at agentpp.org" <snmp4j at agentpp.org>
Sent: Tue, August 17, 2010 1:00:01 PM
Subject: Re: [SNMP4J] Socket  closed error in Windows 7

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