SNMP4J服务端连接超时问题解决方案

 更新时间:2020年10月20日 11:57:00   作者:cuisuqiang  
这篇文章主要介绍了SNMP4J服务端连接超时问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

我们的网络管理中心作为管理中心,是服务端!各个被管设备通过交换机作为客户端与网管中心进行通信,使用的TCP/IP协议!

SNMP只是一种协议包,SNMP4J作为SNMP使用的Java工具包,提供了方便安全的工具包功能!

但是在使用中发现一个问题就是,服务端与客户端发送消息时,发送数次后就不再发送数据了!网络抓包也抓不到,跟踪断点到SNMP4J的代码中发现了这样一个问题!

	/**
	 * 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 sent.
	 * @throws IOException
	 */
	public void sendMessage(Address address, byte[] message)
			throws java.io.IOException {
		if (server == null) {
			listen();
		}
		serverThread.sendMessage(address, message);
	}

我们可以看到,他与UDP的不同是,使用了一个服务的线程!

	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();
		}
	}

他从一个Map中去获得连接 SocketEntry ,然后得到连接对象Socket!

判断Socket是否有效,有效则直接发送,无效则创建连接后再发送!

然后我找到这样一段代码

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

也就是说服务端会自己检查的连接并且去清除他!

我尝试设置 connectionTimeout 的值

private void init() throws UnknownHostException, IOException { 
  threadPool = ThreadPool.create("Trap", 2); 
  dispatcher = new MultiThreadedMessageDispatcher(threadPool,new MessageDispatcherImpl()); 
  // 本地IP与监听端口 
  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(); 
} 

增加一行代码 设置DefaultTcpTransportMapping的超时时间是 0 !

然后就没有问题了!

虽然临时解决了问题,但是由于对SNMP4J不够深入了解,我怕问题恐怕不是这样的!

我在此也希望使用SNMP4J为工具,且作为服务端,在发送数据时有问题的解决方法!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决服务器运行jupyter notebook方法

    解决服务器运行jupyter notebook方法

    这篇文章主要介绍了解决服务器运行jupyter notebook方法,来帮助大家实现服务器跑Jupyter,附含图文以及详细代码,有需要的朋友可以借鉴参考下
    2021-08-08
  • Spark的广播变量和累加器使用方法代码示例

    Spark的广播变量和累加器使用方法代码示例

    这篇文章主要介绍了Spark的广播变量和累加器使用方法代码示例,文中介绍了广播变量和累加器的含义,然后通过实例演示了其用法,需要的朋友可以参考下。
    2017-09-09
  • 详解微服务架构及其演进史

    详解微服务架构及其演进史

    在很多项目的业务初期阶段,高速迭代上线是首要考虑的事情,对后期的容量预估、可扩展性和系统健壮性、高可用一般没有那么重视。但随着业务的发展,用户量、请求量的暴增发现原来的单体系统已经远远不满足需求了,特别是随着互联网整体的高速发展,对系统的要求越来越高
    2022-01-01
  • RedHat9配置转发DNS服务器的实现

    RedHat9配置转发DNS服务器的实现

    本文主要介绍了RedHat9配置转发DNS服务器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • 基于Nexus实现配置阿里云代理仓库过程解析

    基于Nexus实现配置阿里云代理仓库过程解析

    这篇文章主要介绍了基于Nexus实现配置阿里云代理仓库过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • cwRsync提示password file must be owned by root when running as root的解决方法

    cwRsync提示password file must be owned by root when running as

    今天在配置服务器的时候,用了rsync4.10版本,客户端是2003服务器端是2008 r2 同步的时候提示password file must be owned by root when running as root问题,以前用老版本的时候没见过,还好看了下面的文章解决了,特分享下
    2015-08-08
  • 使用命令远程注销服务器的方法

    使用命令远程注销服务器的方法

    这篇文章主要介绍了使用命令远程注销服务器的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2006-11-11
  • Apache Hudi数据布局黑科技降低一半查询时间

    Apache Hudi数据布局黑科技降低一半查询时间

    这篇文章主要介绍了Apache Hudi数据布局黑科技帮你降低一半查询时间,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-03-03
  • rsync同步数据时提示password file must not be other-accessible的解决方法

    rsync同步数据时提示password file must not be&nb

    今天服务器同步数据的时候,突然有个命令提示这个错误,但其它的机器又正常,很奇怪,不过通过下面的命令执行以下就可以了,windows与linux操作方法一致
    2024-06-06
  • 近期服务器出现的安全问题以及防范措施2017.05

    近期服务器出现的安全问题以及防范措施2017.05

    近期接到idc商的反馈,最近很多使用windows的主机都被拿下控制权,直接修改iis等
    2017-08-08

最新评论