2016-06-25 5 views
0

私はJavaのAsynchronousServerSocketChannelをテストしていますが、私は真= SO_KEEPALIVEを設定しようとしたとき、エラーメッセージがそれをサポートしていないことを教えてくれましたか?以下の問題を解決するには?それは本当にubunutサーバーでキープアライブをサポートしていませんか?java aio SO_KEEPALIVEはubuntu 16.04ではサポートされていませんか?

コードは次のとおりです。

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) 

答えて

1

あなたが唯一の新しい着信接続を受け入れることができず、リスニングソケットすなわち、非同期サーバーのSocketChannelを使用していますデータを交換する。ソケットオプションは、単にリスニングソケットには適用されません。

class documentation状態限り。

あなたはオプションSO_KEEPALIVEとTCP_NODELAYに関するクライアントのソケットにのみ適用を意味し、あなたがaccept()

+0

から取得する新しいソケットにそれらのオプションを設定する必要がありますか? –

+0

ちょっと試してみてください。 – the8472

+0

私はTCP/IPに慣れていないので、OKです。 –

関連する問題