クライアントからの1000以上の接続を処理できるサーバーを作成しようとしています。これは一部アカデミック、一部趣味のプロジェクトなので、自分で解決策を作りたいのですが、問題に直面しています。接続をリッスンし始め、誰かが接続すると、TCP接続が5秒後にJavaによって閉じられます。これは私の5秒間の睡眠からのものであることは知っていますが、それを取り除くとすぐに戻ります。
これが私のサーバーコードです(削除されました):
final int SERVER_PORT = 9000;
final String SERVER_IP = "10.0.0.201";
AsynchronousChannelGroup group = null;
try {
group = AsynchronousChannelGroup.withThreadPool(threadPool);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create asynchronous server-socket channel bound to the default group.
try(AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel
.open(group)) {
if ( asynchronousServerSocketChannel.isOpen() ) {
// Bind to local address
asynchronousServerSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT),
SERVER_SOCKET_CHANNEL_BACKLOG);
// Display a waiting message
System.out.println("Waiting for connections on ip:port " + SERVER_IP + ":" + SERVER_PORT);
while (true) { // Not good?
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel
.accept();
try(AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get()) {
final SocketAddress remoteAddress = asynchronousSocketChannel.getRemoteAddress();
System.out.println("Incoming connection from: " + remoteAddress);
final ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);
// Time to receive data.
asynchronousSocketChannel.read(incomingBuffer, incomingBuffer,
new CompletionHandler<Integer, ByteBuffer>() {
public void completed( Integer result, ByteBuffer buffer ) {
}
public void failed( Throwable exc, ByteBuffer buffer ) {
if ( exc instanceof AsynchronousCloseException ) {
// Someone closed the connection
// while we where listening on it.
System.out.println("We listened on the socket, but someone closed it.");
}
}
});
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e.toString());
}
} catch (IOException | InterruptedException | ExecutionException ex) {
System.err.println(ex);
}
}
} else {
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
} catch (IOException ex) {
System.err.println(ex);
}
}
このコードを実行し、netcat "nc 10.0.0.201 9000"で接続すると、接続は5秒後にJava /サーバー側からリセットされます(スリープを解除した場合はすぐに)。
どうすればそれが戻ってくるのを止めて、それを聴き続けることができますか?私は最初の目標を解決するために正しいアプローチを取っていますか?