最小限の例を書きましたが、それでも長すぎるので、代わりに 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()
しかし、これらのを削除するとSrv.runInstance()
、次のようになります。
Connection cannot be established
Launched master on 127.0.0.1:9001
Conn from:/127.0.0.1:50438
では、クライアントはサーバーに接続するが、サーバーは拒否するということですか? ここで何が起こっているのかはっきりと理解できず、ドキュメントも貧弱なので、どこで解決策を探すべきかわかりません。