0
誰能告訴我爲什麼我在得到此異常sc.connect
ClosedChannelException
(SC =的SocketChannel)
/**
* Verify socket is connected. If not currently connected will attempt to connect
* @return true if already connected or connection successfully established
*/
private synchronized boolean verifyConnection() {
if (sc.isConnected()) {
return true;
}
try {
if (!sc.isOpen()) {
logger.info("Channel WebBroker->CC is CLOSED unexpectedly. Opening new channel " + getIpAddress() + ":" + getIpPort() + "");
openChannel();
}
sc.socket().close();
sc.connect(new InetSocketAddress(getIpAddress(), getIpPort()));
while(!sc.finishConnect()){
Thread.sleep(1);
}
logger.info("Connection established " + getIpAddress() + ":" + getIpPort());
sc.socket().setKeepAlive(true);
return sc.isConnected();
} catch (Exception e) {
logger.info("failed to connect to " + getIpAddress() + ":" + getIpPort(), e);
return false;
}
}
private void openChannel() throws Exception {
sc = SocketChannel.open();
sc.socket().setKeepAlive(true);
sc.socket().setSoTimeout(30000);
}
[錯誤]無法連接到10.201.1.53:8084
java.nio.channels.ClosedChannelException: null
at sun.nio.ch.SocketChannelImpl.ensureOpenAndUnconnected(SocketChannelImpl.java:472) ~[na:1.6.0_07]
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:486) ~[na:1.6.0_07]
編輯:最後我發現這是因爲下面的線。
sc.socket().close();
嗨,我正在重新連接?請告訴我如何創建一個新的套接字而不是重新連接? – 2013-03-14 21:57:51