• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Client NIO socket read problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having problems tuning a read operation. This method actually is
invoked by a client call, so it doesn't register OP_ACCEPT or anything
like that.

The problem is that this method's completion time is equivalent to the
Selector.select(milliseconds) time. If its 5000, it completes in 5
seconds. 1000 results in one second. I'd really like it do the read
and break out of the loop when its ready, somehow.

private String readFromChannel (SocketChannel
sChannel) throws IOException
{
CharsetDecoder decoder =
this.ascii.newDecoder();
ByteBuffer buffer = ByteBuffer.allocate(2048);
CharBuffer charBuffer =
CharBuffer.allocate(2048);
StringBuffer dataRead = new StringBuffer();
Selector selector = Selector.open();
boolean hasRead = false;
// Register read activity on socket
sChannel.register(selector,
SelectionKey.OP_READ);
// Read response with 1000 mililiseconds timeout
while (selector.select(1000) > 0)
{
System.out.println("selector looping...");
// Get set of ready objects
Set readyKeys = selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();
// Walk through set
while (readyItor.hasNext())
{
System.out.println("iterator looping...");
// Get key from set
SelectionKey key =
(SelectionKey)readyItor.next();
// Remove current entry
readyItor.remove();
// Get channel
SocketChannel keyChannel =
(SocketChannel)key.channel();
if (key.isReadable())
{
// Read what's ready in response
while (keyChannel.read(buffer) > 0)
{
// Make buffer readable
buffer.flip();
// Decode buffer
decoder.decode(buffer, charBuffer,
false);
// Build string recieved
charBuffer.flip();
dataRead.append(charBuffer);
// Clear for next pass
buffer.clear();
charBuffer.clear();
// Indicate successful operation
System.out.println("length of data read:
" + dataRead.toString().length());
hasRead = true;
}
}//end key.isReadable()
}//end while iterator
}//end while selector

if (false == hasRead)
{
throw new IllegalStateException ("Socket read
operation timed out");
}

return dataRead.toString();
}

Any ideas?
iksrazal
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by trebor iksrazal:
I'm having problems tuning a read operation. This method actually is
invoked by a client call, so it doesn't register OP_ACCEPT or anything
like that.

The problem is that this method's completion time is equivalent to the
Selector.select(milliseconds) time. If its 5000, it completes in 5
seconds. 1000 results in one second. I'd really like it do the read
and break out of the loop when its ready, somehow.
...
iksrazal



Your code looks fine, I posted a working NIO Clienton the Sun Java forums. It is a Swing based client that sends and receives character strings.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic