gpt4 book ai didi

ubuntu 16.04 不支持 java aio SO_KEEPALIVE?

转载 作者:行者123 更新时间:2023-12-02 03:31:33 25 4
gpt4 key购买 nike

我正在测试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() 获得的新套接字上设置这些选项

关于ubuntu 16.04 不支持 java aio SO_KEEPALIVE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com