任何人都告诉我为什么我会在sc.connect
( 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();