Why do asynchronous channels in Java's NIO.2 require these pauses? -
i've written minimal example, it's still long, let me know should post link pastebin instead.
server:
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(); } { 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 opened"); } } catch (ioexception e) { e.printstacktrace(); } } }
client:
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(); } { try { asynchronoussocketchannel.close(); } catch (ioexception exn) { exn.printstacktrace(); } } } @override public void failed(throwable throwable, void o) { system.out.println("connection cannot established"); } }); sleep(1000); //and here } else { system.out.println("the asynchronous socket channel cannot opened"); } } catch (ioexception e) { e.printstacktrace(); } } }
boilerplate run:
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(); } }
so, if have code this, gives me following output:
launched master on 127.0.0.1:9001 connected to: 127.0.0.1 9001 conn from:/127.0.0.1:50415
but if remove sleep()
s in srv.runinstance()
, get:
connection cannot established launched master on 127.0.0.1:9001 conn from:/127.0.0.1:50438
so, mean client connects server, server refuses? don't understand happens here , documentation rather poor, don't know solutions.
the sleep see in posted code in wrk.runinstance. however, order of output makes pretty clear going on. working trying connect server before has initialized. can see in output, "connection message" before "launched master".
the server takes bit of time start, without sleep, client trying connect not yet there.
Comments
Post a Comment