Its Not Okay Posted August 5, 2019 Share Posted August 5, 2019 Trying to learn java async sockets, I send a request for bot login to the server, get the login from the db and send it back however instead of the client printing out the server response it just reprints the request: Server: if ((client != null) && (client.isOpen())) { System.out.println("Client connected."); ByteBuffer buffer = ByteBuffer.allocate(1024); Future<Integer> readVal = client.read(buffer); String clientInput = new String(buffer.array()).trim(); System.out.println("Received from client: " + clientInput); readVal.get(); buffer.flip(); statements.getCase(clientInput); System.out.println(responseString); Future<Integer> writeVal = client.write(ByteBuffer.wrap(responseString.getBytes())); System.out.println("Writing back to client: " + responseString); writeVal.get(); buffer.clear(); } Server log: Server is listening on port: 5000 Client connected. Received from client: Requesting Bot Login Connected to the PostgreSQL server successfully. Writing back to client: test:password Client: try (AsynchronousSocketChannel client = AsynchronousSocketChannel.open()) { Future<Void> result = client.connect(new InetSocketAddress("127.0.0.1", 5000)); result.get(); String str = "Requesting Login"; ByteBuffer buffer = ByteBuffer.wrap(str.getBytes()); Future<Integer> writeval = client.write(buffer); System.out.println("Writing to server: " + str); writeval.get(); buffer.flip(); Future<Integer> readval = client.read(buffer); String serverResponse = new String(buffer.array()).trim(); System.out.println("Received from server: " + serverResponse); readval.get(); buffer.clear(); } Client log: Writing to server: Requesting Bot Login Received from server: Requesting Bot Login Both server and client listening on port 5000. Quote Link to comment Share on other sites More sharing options...
dreameo Posted August 5, 2019 Share Posted August 5, 2019 Hmm I haven't used async sockets but my guess would be is that since it's nonblocking (not sure if this is true), you're making a request and reading that request instantaneously when it isn't ready. Try putting a Thread.sleep(1000) in between the request and the read in the client side and see if you get a valid response then. Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 6, 2019 Share Posted August 6, 2019 Future<Integer> readval = client.read(buffer); String serverResponse = new String(buffer.array()).trim(); System.out.println("Received from server: " + serverResponse); readval.get(); You cannot read the serverResponse immediately after requesting the read. It's called Async for a reason, and returns a 'Future' for the very same reason. readval.get() will block until the read is completed, after which you may read the response. Not before. To properly utilize the async-aspect of it, you should get the response at some point after readval.isDone() is true, or better yet retrieve the response from a completionhandler by using the overload AsynchronousSocketChannel#read(ByteBuffer, Attachment, CompletionHandler). The completionhandler will execute once the result is ready. 4 Quote Link to comment Share on other sites More sharing options...