Jump to content

Java Asynchronous Sockets


Its Not Okay

Recommended Posts

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

            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.

  • Like 4
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...