aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Reading data from server at client side asynchronously (nonblocking) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Reading data from server at client side asynchronously (nonblocking)" Watch "Reading data from server at client side asynchronously (nonblocking)" New topic
Author

Reading data from server at client side asynchronously (nonblocking)

Amee Dabo
Ranch Hand

Joined: Dec 22, 2001
Posts: 74
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 = Selector.open();
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(false);
sChannel.connect(new InetSocketAddress("ipaddr",port));
sChannel.register(selector, SelectionKey.OP_CONNECT);

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


//Read from the server
ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024);
int numReadBytes = sChannel.read(readBuffer);
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,
Ameeta


Thanks..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading data from server at client side asynchronously (nonblocking)
 
Similar Threads
ServerSocketChannel.accept() seems to create a random port to send data to?
unable to read data from server using socketchannle (help please)
NIO Socket weirdnes in Solaris 2.10
does a nio socket buffer data internally too?
client doesn't detect server drop