时间:2020-10-20来源:www.pcxitongcheng.com作者:电脑系统城
我们的网络管理中心作为管理中心,是服务端!各个被管设备通过交换机作为客户端与网管中心进行通信,使用的TCP/IP协议!
SNMP只是一种协议包,SNMP4J作为SNMP使用的Java工具包,提供了方便安全的工具包功能!
但是在使用中发现一个问题就是,服务端与客户端发送消息时,发送数次后就不再发送数据了!网络抓包也抓不到,跟踪断点到SNMP4J的代码中发现了这样一个问题!
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 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的不同是,使用了一个服务的线程!
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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是否有效,有效则直接发送,无效则创建连接后再发送!
然后我找到这样一段代码
?1 2 3 4 5 |
private synchronized void timeoutSocket(SocketEntry entry) { if (connectionTimeout > 0 ) { socketCleaner.schedule( new SocketTimeout(entry), connectionTimeout); } } |
也就是说服务端会自己检查的连接并且去清除他!
我尝试设置 connectionTimeout 的值
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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不够深入了解,我怕问题恐怕不是这样的!
2023-03-17
Nginx主机域名配置实现2023-03-17
Nginx配置-日志格式配置方式2023-03-17
Apache APISIX Dashboard 未授权访问漏洞分析(CVE-2021-45232)正常项目无法访问(Linux 服务器),启动tomcat时卡在下图位置,项目无法启动。1、先检查tomcat日志、项目日志没有报错信息,且没再产生新的日志信息。2、jdk、tomcat 、 jvm 配置,服...
2023-03-15
这篇文章主要介绍了Kubernetes中Nginx服务启动失败排查流程(Error: ImagePullBackOff),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以...
2023-03-15