class CheckServerStarted implements Closeable
{
private final SocketChannel channel;
private final InetSocketAddress address;
private boolean connStarted = false;
public CheckServerStarted(InetSocketAddress address) throws IOException
{
channel = SocketChannel.open();
channel.configureBlocking(false);
this.address = address;
}
/**
* @throws IOException
*
*/
private void startConn() throws IOException
{
channel.connect(address);
connStarted = true;
}
private boolean check(int timeout, boolean retry)
{
for (int i = 0; i < timeout; i++)
{
try
{
final boolean justStarted = !connStarted;
if (justStarted)
{
startConn();
}
if (justStarted || (i > 0))
{
// Wait a second between attempts/give the connection some time to get established.
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
if (channel.finishConnect())
{
System.out.println("Server started - accepting connections on " + address.toString());
return true;
}
}
catch (IOException e)
{
System.out.println("Connect attempt failed : " + e.getMessage());
if (!retry)
{
break;
}
// Try to start the connection again if it failed.
connStarted = false;
}
}
System.out.println("Connect attempt to " + address.toString() + " failed, ran out of time/attempts");
return false;
}
/* (non-Javadoc)
* @see java.io.Closeable#close()
*/
public void close()
{
try
{
channel.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
我有以下代码,用于等待/检查服务器启动&amp;正在听一个给定的地址/端口。
我的想法是,我可以,例如,呼叫
new CheckServerStarted(addr).check(60, true);
尝试连接它最多60秒(如果连接失败,则重试,因为服务器尚未绑定套接字)
或
new CheckServerStarted(addr).check(1, false);
检查服务器当前是否正在接受连接。
当我现在运行第一个例子时,我得到像
这样的输出Connect attempt failed : Connection refused: no further information
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt failed : null
14-Jun-2018 10:55:42 Connect attempt to /192.168.223.68:59695 failed, ran out of time/attempts
显然不会在尝试之间等待1秒。
我猜我需要在两次尝试之间“重置”频道?我是否每次都需要创建一个新的?
答案 0 :(得分:1)
即使连接尝试失败,也无法重新连接已连接的套接字。必须关闭它并创建一个新的。
其他问题:
Selector
。e
而不是e.getMessage()
。如果您首先正确地做到了这一点,而不是仅仅盯着&#39; null&#39;整天,你可能已经自己解决了这个问题。IOException
来电finishConnect()
是致命的,而不是重试的信号。