- 2023-10-11 12:11:09
- 2479 热度
- 0 评论
我们的网络管理中心作为管理中心,是服务端!各个被管设备通过交换机作为客户端与网管中心进行通信,使用的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为工具,且作为服务端,在发送数据时有问题的解决方法!
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- session(8)
- Window(8)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- Cache(7)
- File(7)
- mail(7)
- IntelliJ(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- ueditor(6)
- jar(6)
- UDP(6)
- ehcache(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- Emlog(5)
- discuz(5)