简体   繁体   English

AsynchronousServerSocketChannel.accept仅接受一个连接

[英]AsynchronousServerSocketChannel.accept is only accepting a one connection

I have a small server setup where I'm trying to use event based connection sockets, so that a handler is called on each incoming connection. 我有一个小型服务器设置,试图使用基于事件的连接套接字,以便在每个传入的连接上调用一个处理程序。 It works great for the first connection, but no new connections are accepted after the first one. 它对于第一个连接非常有效,但是在第一个连接之后不接受任何新的连接。

I just close the client connection when it comes in for simplicity. 为了简单起见,我只是关闭了客户端连接。 Also, yes the server is still running after the first connection, it doesn't terminate. 此外,是的,服务器在第一次连接后仍在运行,它不会终止。

Here is the code: 这是代码:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class ServerTest
{
static CompletionHandler<AsynchronousSocketChannel, Object> handler =
        new CompletionHandler<AsynchronousSocketChannel, Object>() {
        @Override
        public void completed(AsynchronousSocketChannel result, Object attachment) {
            System.out.println(attachment + " completed with " + result + " bytes written");
            try {
                result.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void failed(Throwable e, Object attachment) {
            System.err.println(attachment + " failed with:");
            e.printStackTrace();
        }
    };

public static void main(String[] args) throws Exception
{
    AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(Executors.newSingleThreadExecutor());
    System.out.println("STARTING");
    AsynchronousServerSocketChannel ssc =
        AsynchronousServerSocketChannel.open(group).bind(new InetSocketAddress("localhost", 9999));

    System.out.println("BOUND");
    ssc.accept(ssc, handler);

    group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

}
}

public abstract void accept(A attachment, CompletionHandler handler) 公共抽象无效接受(附件,CompletionHandler处理程序)

This method initiates an asynchronous operation to accept a connection made to this channel's socket. 此方法启动异步操作,以接受与此通道的套接字建立的连接。 The handler parameter is a completion handler that is invoked when a connection is accepted (or the operation fails). 该处理程序参数是一个完成处理程序,当接受连接(或操作失败)时调用。 The result passed to the completion handler is the AsynchronousSocketChannel to the new connection. 传递给完成处理程序的结果是到新连接的A​​synchronousSocketChannel。

Read more here 在这里阅读更多

This means that it initializes an asynchronous thread to accept incoming connections. 这意味着它将初始化异步线程以接受传入的连接。 This also means that it'll take the first connection and forward it to the asynchronous thread and then wait for more connections. 这也意味着它将获取第一个连接并将其转发到异步线程,然后等待更多连接。 To allow more clients to connect you must invoke an accept method inside of the overwritten completed function as well. 要允许更多客户端连接,您还必须在覆盖的已完成函数内部调用一个accept方法。

Here is an example, 这是一个例子

server.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
@Override
public void completed(AsynchronousSocketChannel chan, Void attachment) {
    System.out.println("Incoming connection...");
    server.accept(null, this); //add this

    //...

It should be noted that for every client, a new AsynchronousSocketChannel result is produced. 应当注意,对于每个客户端,都会产生一个新的AsynchronousSocketChannel结果。 That being said if you were to print out 'chan', it'll result in different objects. 话虽这么说,如果您要打印“ chan”,它将导致不同的对象。

Distinct client objects 不同的客户端对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM