Solution to SNMP4J server connection timeout problem

Solution to SNMP4J server connection timeout problem

Our network management center serves as the management center and the server! Each managed device communicates with the network management center through the switch as a client, using the TCP/IP protocol!

SNMP is just a protocol package. SNMP4J, as a Java toolkit used by SNMP, provides convenient and secure toolkit functions!

However, a problem was found during use: when the server and the client send messages, they no longer send data after sending several times! The network packet capture was also unsuccessful, and a problem was discovered in the SNMP4J code after tracing the breakpoint!

	/**
	 * Sends a SNMP message to the supplied address.
	 * 
	 * @param address
	 * an <code>TcpAddress</code>. A
	 * <code>ClassCastException</code> is thrown if
	 * <code>address</code> is not a <code>TcpAddress</code>
	 * instance.
	 * @param message
	 * byte[] the message to be 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 it and UDP is that it uses a service thread!

	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 connection SocketEntry from a Map, and then gets the connection object Socket!

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

Then I found this code

private synchronized void timeoutSocket(SocketEntry entry) { 
  if (connectionTimeout > 0) { 
    socketCleaner.schedule(new SocketTimeout(entry), connectionTimeout); 
  } 
}

That is to say, the server will check the connection itself and clear it!

I tried setting the value of connectionTimeout

private void init() throws UnknownHostException, IOException { 
  threadPool = ThreadPool.create("Trap", 2); 
  dispatcher = new MultiThreadedMessageDispatcher(threadPool,new MessageDispatcherImpl()); 
  // Local IP and listening 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 timeout of DefaultTcpTransportMapping to 0!

Then there will be no problem!

Although the problem has been temporarily solved, due to my lack of in-depth understanding of SNMP4J, I am afraid that the problem may not be this!

I also hope to use SNMP4J as a tool, and as a server, there is a solution to the problem when sending data!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem of MySQL server actively disconnecting when there is no operation timeout
  • Solution to the timeout of the mini program server requesting the WeChat server
  • Solution to the timeout problem of curl and soap request service in php
  • A brief discussion on service exceptions caused by asynchronous multithreading timeout in Java
  • Detailed explanation of how to configure timeout in Nginx server
  • The Win7 system log prompts that the name resolution timeout of the name "domain name" has expired after no configured DNS server responds.
  • Solution for ORA-12170 TNS: Connection timeout when connecting to Oracle remote server
  • Timeout when connecting to the server using FileZilla

<<:  The specific use and difference between attribute and property in Vue

>>:  The difference between delete, truncate, and drop and how to choose

Recommend

Useful codes for web page creation

<br />How can I remove the scroll bar on the...

Implementing Binary Search Tree in JavaScript

The search binary tree implementation in JavaScri...

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...

How to install babel using npm in vscode

Preface The previous article introduced the insta...

Web Design Tutorial (4): About Materials and Expressions

<br />Previous Web Design Tutorial: Web Desi...

Website construction experience summary

<br />What principles should be followed to ...

The process of building and configuring the Git environment in Docker

Configure Git environment in Docker At work, I en...

A brief discussion on Linux virtual memory

Table of contents origin Virtual Memory Paging an...

How to bind Docker container to external IP and port

Docker allows network services to be provided by ...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

Detailed explanation of how to exit Docker container without closing it

After entering the Docker container, if you exit ...

Detailed explanation of Linux text processing tools

1. Count the number of users whose default shell ...