SNMP4J服務(wù)端連接超時(shí)問題解決方案
我們的網(wǎng)絡(luò)管理中心作為管理中心,是服務(wù)端!各個(gè)被管設(shè)備通過交換機(jī)作為客戶端與網(wǎng)管中心進(jìn)行通信,使用的TCP/IP協(xié)議!
SNMP只是一種協(xié)議包,SNMP4J作為SNMP使用的Java工具包,提供了方便安全的工具包功能!
但是在使用中發(fā)現(xiàn)一個(gè)問題就是,服務(wù)端與客戶端發(fā)送消息時(shí),發(fā)送數(shù)次后就不再發(fā)送數(shù)據(jù)了!網(wǎng)絡(luò)抓包也抓不到,跟蹤斷點(diǎn)到SNMP4J的代碼中發(fā)現(xiàn)了這樣一個(gè)問題!
/**
* 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的不同是,使用了一個(gè)服務(wù)的線程!
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();
}
}
他從一個(gè)Map中去獲得連接 SocketEntry ,然后得到連接對(duì)象Socket!
判斷Socket是否有效,有效則直接發(fā)送,無效則創(chuàng)建連接后再發(fā)送!
然后我找到這樣一段代碼
private synchronized void timeoutSocket(SocketEntry entry) {
if (connectionTimeout > 0) {
socketCleaner.schedule(new SocketTimeout(entry), connectionTimeout);
}
}
也就是說服務(wù)端會(huì)自己檢查的連接并且去清除他!
我嘗試設(shè)置 connectionTimeout 的值
private void init() throws UnknownHostException, IOException {
threadPool = ThreadPool.create("Trap", 2);
dispatcher = new MultiThreadedMessageDispatcher(threadPool,new MessageDispatcherImpl());
// 本地IP與監(jiān)聽端口
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();
}
增加一行代碼 設(shè)置DefaultTcpTransportMapping的超時(shí)時(shí)間是 0 !
然后就沒有問題了!
雖然臨時(shí)解決了問題,但是由于對(duì)SNMP4J不夠深入了解,我怕問題恐怕不是這樣的!
我在此也希望使用SNMP4J為工具,且作為服務(wù)端,在發(fā)送數(shù)據(jù)時(shí)有問題的解決方法!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決mysql服務(wù)器在無操作超時(shí)主動(dòng)斷開連接的情況
- 小程序server請(qǐng)求微信服務(wù)器超時(shí)的解決方法
- php中curl和soap方式請(qǐng)求服務(wù)超時(shí)問題的解決
- 淺談java中異步多線程超時(shí)導(dǎo)致的服務(wù)異常
- 詳解Nginx服務(wù)器中配置超時(shí)時(shí)間的方法
- Win7系統(tǒng)日志提示在沒有配置的 DNS 服務(wù)器響應(yīng)之后,名稱“域名”的名稱解析超時(shí)的解放方法
- oracle遠(yuǎn)程連接服務(wù)器出現(xiàn) ORA-12170 TNS:連接超時(shí) 解決辦法
- 使用FileZilla連接時(shí)超時(shí)無法連接到服務(wù)器
相關(guān)文章
解決服務(wù)器運(yùn)行jupyter notebook方法
這篇文章主要介紹了解決服務(wù)器運(yùn)行jupyter notebook方法,來幫助大家實(shí)現(xiàn)服務(wù)器跑Jupyter,附含圖文以及詳細(xì)代碼,有需要的朋友可以借鑒參考下2021-08-08
RedHat9配置轉(zhuǎn)發(fā)DNS服務(wù)器的實(shí)現(xiàn)
本文主要介紹了RedHat9配置轉(zhuǎn)發(fā)DNS服務(wù)器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
基于Nexus實(shí)現(xiàn)配置阿里云代理倉(cāng)庫(kù)過程解析
這篇文章主要介紹了基于Nexus實(shí)現(xiàn)配置阿里云代理倉(cāng)庫(kù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
cwRsync提示password file must be owned by root when running as
今天在配置服務(wù)器的時(shí)候,用了rsync4.10版本,客戶端是2003服務(wù)器端是2008 r2 同步的時(shí)候提示password file must be owned by root when running as root問題,以前用老版本的時(shí)候沒見過,還好看了下面的文章解決了,特分享下2015-08-08
Apache Hudi數(shù)據(jù)布局黑科技降低一半查詢時(shí)間
這篇文章主要介紹了Apache Hudi數(shù)據(jù)布局黑科技幫你降低一半查詢時(shí)間,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03
rsync同步數(shù)據(jù)時(shí)提示password file must not be&nb
今天服務(wù)器同步數(shù)據(jù)的時(shí)候,突然有個(gè)命令提示這個(gè)錯(cuò)誤,但其它的機(jī)器又正常,很奇怪,不過通過下面的命令執(zhí)行以下就可以了,windows與linux操作方法一致2024-06-06
近期服務(wù)器出現(xiàn)的安全問題以及防范措施2017.05
近期接到idc商的反饋,最近很多使用windows的主機(jī)都被拿下控制權(quán),直接修改iis等2017-08-08

