java ubuntu 16.04 不支持 aio SO_KEEPALIVE?
java aio SO_KEEPALIVE not supported in ubuntu 16.04?
我正在测试 java AsynchronousServerSocketChannel,但是当我尝试设置 SO_KEEPALIVE=true 时,一条错误消息显示我不支持它?如何解决以下问题? ubunut服务器真的不支持keep-alive吗?
密码是:
private void init(String host, int port) {
try {
final AsynchronousChannelGroup group = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10);
final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(host, port))
.setOption(StandardSocketOptions.SO_KEEPALIVE, true)
.setOption(StandardSocketOptions.TCP_NODELAY, true)
.setOption(StandardSocketOptions.SO_REUSEADDR, true)
.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024);
System.out.println("Listening on: " + host + ":" + port);
System.out.println("Channel Provider : " + server.provider());
server.accept(null, new handler());
group.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
//TimeUnit.DAYS.sleep(Long.MAX_VALUE);
} catch (IOException | InterruptedException ex) {
Logger.getLogger(AIOEchoServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
private class handler implements CompletionHandler<AsynchronousSocketChannel, Void> {
@Override
public void completed(AsynchronousSocketChannel result, Void attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void failed(Throwable ex, Void attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
错误是:
Exception in thread "main" java.lang.UnsupportedOperationException: 'SO_KEEPALIVE' not supported
at sun.nio.ch.AsynchronousServerSocketChannelImpl.setOption(AsynchronousServerSocketChannelImpl.java:187)
at TestAIO.AIOEchoServer.init(AIOEchoServer.java:28)
at TestAIO.AIOEchoServer.main(AIOEchoServer.java:20)
您正在使用异步服务器SocketChannel,即只能接受新传入连接而不能交换数据的侦听套接字。套接字选项根本不适用于监听套接字。
class documentation 说的也是如此。
您应该在从 accept()
获得的新套接字上设置这些选项
我正在测试 java AsynchronousServerSocketChannel,但是当我尝试设置 SO_KEEPALIVE=true 时,一条错误消息显示我不支持它?如何解决以下问题? ubunut服务器真的不支持keep-alive吗?
密码是:
private void init(String host, int port) {
try {
final AsynchronousChannelGroup group = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10);
final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(host, port))
.setOption(StandardSocketOptions.SO_KEEPALIVE, true)
.setOption(StandardSocketOptions.TCP_NODELAY, true)
.setOption(StandardSocketOptions.SO_REUSEADDR, true)
.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024);
System.out.println("Listening on: " + host + ":" + port);
System.out.println("Channel Provider : " + server.provider());
server.accept(null, new handler());
group.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
//TimeUnit.DAYS.sleep(Long.MAX_VALUE);
} catch (IOException | InterruptedException ex) {
Logger.getLogger(AIOEchoServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
private class handler implements CompletionHandler<AsynchronousSocketChannel, Void> {
@Override
public void completed(AsynchronousSocketChannel result, Void attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void failed(Throwable ex, Void attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
错误是:
Exception in thread "main" java.lang.UnsupportedOperationException: 'SO_KEEPALIVE' not supported
at sun.nio.ch.AsynchronousServerSocketChannelImpl.setOption(AsynchronousServerSocketChannelImpl.java:187)
at TestAIO.AIOEchoServer.init(AIOEchoServer.java:28)
at TestAIO.AIOEchoServer.main(AIOEchoServer.java:20)
您正在使用异步服务器SocketChannel,即只能接受新传入连接而不能交换数据的侦听套接字。套接字选项根本不适用于监听套接字。
class documentation 说的也是如此。
您应该在从 accept()