首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ubuntu16.04中不支持java SO_KEEPALIVE?

在ubuntu16.04中不支持java SO_KEEPALIVE?
EN

Stack Overflow用户
提问于 2016-06-25 12:31:02
回答 1查看 437关注 0票数 0

我正在测试java AsynchronousServerSocketChannel,但是当我尝试set SO_KEEPALIVE=true时,一条错误消息告诉我不支持它?如何解决以下问题?它真的不支持在ubunut服务器中保持生存吗?

守则是:

代码语言:javascript
复制
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.
    }
}

错误是:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-25 12:50:21

您使用的是AsynchronousServerSocketChannel,,即侦听套接字,它只能接受新的传入连接,而不能交换数据。套接字选项根本不适用于侦听套接字。

类文档的声明也是如此。

您应该在从accept()获得的新套接字上设置这些选项。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38028550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档