Timeout value for SNMP4J server connection

Source: Internet
Author: User
Tags snmp

Our network management center acts as the management center and serves as the server! The TCP/IP protocol used by each managed device to communicate with the network management center through a vswitch!

SNMP is only a protocol package. As a Java toolkit for SNMP, SNMP4J provides convenient and secure toolkit functions!

 

However, a problem found during use is that when the server and client send messages, the data will not be sent after several times! Network Packet Capture fails. This problem is found in the code that traces breakpoints to SNMP4J!

 

[Java]
/**
* Sends a SNMP message to the supplied address.
*
* @ Param address
* An <code> TcpAddress </code>.
* <Code> ClassCastException </code> is thrown if
* <Code> address </code> is not a <code> TcpAddress </code>
* Instance.
* @ Param message
* Byte [] the message to sent.
* @ Throws IOException
*/
Public void sendMessage (Address address, byte [] message)
Throws java. io. IOException {
If (server = null ){
Listen ();
}
ServerThread. sendMessage (address, message );
}

We can see that the difference between him and UDP is that a service thread is used!

[Java]
Public void sendMessage (Address address, byte [] message)
Throws java. io. IOException {
Socket s = null;
SocketEntry entry = (SocketEntry) sockets. get (address );
If (logger. isDebugEnabled ()){
Logger. debug ("Looking up connection for destination '"
+ Address + "'returned:" + entry );
Logger. debug (sockets. toString ());
}
If (entry! = Null ){
S = entry. getSocket ();
}
If (s = null) | (s. isClosed () | (! S. isConnected ())){
If (logger. isDebugEnabled ()){
Logger. debug ("Socket for address '" + address
+ "'Is closed, opening it ...");
}
Pending. remove (entry );
SocketChannel SC = null;
Try {
// Open the channel, set it to non-blocking, initiate
// Connect
SC = SocketChannel. open ();
SC. configureBlocking (false );
SC
. Connect (new InetSocketAddress (
(TcpAddress) address). getInetAddress (),
(TcpAddress) address). getPort ()));
S = SC. socket ();
Entry = new SocketEntry (TcpAddress) address, s );
Entry. addMessage (message );
Sockets. put (address, entry );
 
Synchronized (pending ){
Pending. add (entry );
}
 
Selector. wakeup ();
Logger. debug ("Trying to connect to" + address );
} Catch (IOException iox ){
Logger. error (iox );
Throw iox;
}
} Else {
Entry. addMessage (message );
Synchronized (pending ){
Pending. add (entry );
}
Selector. wakeup ();
}
}

 

He gets the SocketEntry connection from a Map and then gets the connection object Socket!

Determine whether the Socket is valid. If the Socket is valid, send it directly. If the Socket is invalid, create a connection and send it again!

 

Then I found the code

[Java]
Private synchronized void timeoutSocket (SocketEntry entry ){
If (connectionTimeout> 0 ){
SocketCleaner. schedule (new SocketTimeout (entry), connectionTimeout );
}
}

That is to say, the server checks the connection and clears it!

 

I tried to set the connectionTimeout Value

[Java]
Private void init () throws UnknownHostException, IOException {
ThreadPool = ThreadPool. create ("Trap", 2 );
Dispatcher = new MultiThreadedMessageDispatcher (threadPool, new MessageDispatcherImpl ());
// Local IP address and listener Port
ListenAddress = GenericAddress. parse (System. getProperty ("snmp4j. listenAddress", "tcp: 192.168.9.69/5055 "));
DefaultTcpTransportMapping transport;
Transport = new DefaultTcpTransportMapping (TcpAddress) listenAddress );
Transport. setConnectionTimeout (0 );
Snmp = new Snmp (dispatcher, transport );
Snmp. getMessageDispatcher (). addMessageProcessingModel (new MPv1 ());
Snmp. getMessageDispatcher (). addMessageProcessingModel (new MPv2c ());
Snmp. getMessageDispatcher (). addMessageProcessingModel (new MPv3 ());
USM usm = new USM (SecurityProtocols. getInstance (), new OctetString (MPv3.createLocalEngineID (), 0 );
SecurityModels. getInstance (). addSecurityModel (usm );
Snmp. listen ();
}

 

Add a line of code to set the ulttcptransportmapping timeout to 0!

Then there is no problem!

 

Although the problem has been solved temporarily, I am afraid this is not the case because I do not have a deep understanding of SNMP4J!

I also want to use SNMP4J as a tool and as a server to solve problems when sending data!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.