0

我写了最小的例子,但它仍然太长,所以让我知道我应该发布到 Pastebin 的链接。

服务器:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class Srv {
    static final int    PORT = 9001;
    static final String HOST = "127.0.0.1";
    public static void runInstance() {
        try (AsynchronousServerSocketChannel asynchronousServerSocketChannel =
                     AsynchronousServerSocketChannel.open()) {
            if (asynchronousServerSocketChannel.isOpen()) {
                asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024);
                asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
                asynchronousServerSocketChannel.bind(new InetSocketAddress(HOST, PORT));
                System.out.println(String.format("Launched master on %s:%d", HOST, PORT));

                asynchronousServerSocketChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
                    @Override
                    public void completed(AsynchronousSocketChannel result, Void attachment) {
                        asynchronousServerSocketChannel.accept(null, this);
                        try {
                            result.read(ByteBuffer.allocate(1024));
                            System.out.println("Conn from:" + result.getRemoteAddress());
                        } catch (Exception exn) {
                            exn.printStackTrace();
                        } finally {
                            try {
                                result.close();
                            } catch (IOException exn) {
                                exn.printStackTrace();
                            }
                        }
                    }
                    @Override
                    public void failed(Throwable exn, Void attachment) {
                        asynchronousServerSocketChannel.accept(null, this);
                        throw new UnsupportedOperationException("can't accept");
                    }
                });
                System.in.read();
            } else {
                System.out.println("The asynchronous server-socket channel cannot be opened");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

客户:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

import static java.lang.Thread.sleep;

public class Wrk {
    static final int    PORT = 9001;
    static final String HOST = "127.0.0.1";
    public static void runInstance() throws InterruptedException {
        sleep(1000); //HERE
        try(AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open()) {
            if (asynchronousSocketChannel.isOpen()) {
                asynchronousSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024);
                asynchronousSocketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 1024);
                asynchronousSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
                asynchronousSocketChannel.connect(new InetSocketAddress(HOST, PORT), null,
                    new CompletionHandler<Void, Void>() {
                        @Override
                        public void completed(Void result, Void attachment) {
                            try {
                                System.out.println("Connected to: " + HOST + PORT);
                                asynchronousSocketChannel.read(ByteBuffer.allocate(1024)).get();
                            } catch (Exception exn) {
                                exn.printStackTrace();
                            } finally {
                                try {
                                    asynchronousSocketChannel.close();
                                } catch (IOException exn) {
                                    exn.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void failed(Throwable throwable, Void o) {
                            System.out.println("Connection cannot be established");
                        }
                    });
                sleep(1000); //AND HERE
            } else {
                System.out.println("The asynchronous socket channel cannot be opened");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

要运行的样板:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread srv = new Thread(new Runnable() {
            @Override
            public void run() {
                Srv.runInstance();
            }
        });
        srv.start();
        Thread wrk1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Wrk.runInstance();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        wrk1.start();
    }
}

所以,如果我有这样的代码,它会给我以下输出:

Launched master on 127.0.0.1:9001
Connected to: 127.0.0.1 9001
Conn from:/127.0.0.1:50415

但是,如果我删除那些sleep()s in Srv.runInstance(),我会得到:

Connection cannot be established
Launched master on 127.0.0.1:9001
Conn from:/127.0.0.1:50438

那么,这是否意味着客户端连接到服务器,但服务器拒绝?我不清楚这里发生了什么,文档也很差,所以我不知道在哪里寻找解决方案。

4

1 回答 1

1

我在发布的代码中看到的唯一睡眠是在 Wrk.runInstance 中。但是,输出的顺序很清楚发生了什么。您的工作正在尝试在服务器完全初始化之前连接到它。正如您在输出中看到的那样,“连接消息”位于“已启动的主服务器”之前。

服务器需要一些时间才能启动,因此如果没有睡眠,您的客户端会尝试连接到尚未存在的东西。

于 2013-10-06T17:10:15.737 回答