Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reading data from server at client side asynchronously (nonblocking)

Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am writing client side program which needs to read & write data from server asynchronously.

I have written the following code for the same:

Selector selector =;
SocketChannel sChannel =;
sChannel.connect(new InetSocketAddress("ipaddr",port));
sChannel.register(selector, SelectionKey.OP_CONNECT);

//Wait for Events
while ( > 0) {
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
while (i.hasNext()) {
SelectionKey key = (SelectionKey);
if (key.isConnectable()) {
//Write to the server
//Code to write to teh server

//Read from the server
ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024);
int numReadBytes =;
while (numReadBytes != -1) { for (int j=0; j < readBuffer.limit(); j++) {
//read buffer

--> My problem is that I am able to write data from my client appication to teh server but just not able to read the response back from the server.
Tried with n! no of ways but just not able to receive the response ;(

Can you please help?

Many thanks,
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic