Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java Asynchronous Sockets

Featured Replies

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.

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. 

            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.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.