• 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

Reading data from a SocketChannel

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to learn the java nio stuff. What I am trying to do first is make a simple server/client where there is only 1 client and no selectors... just to try and get the non-blocking stuff down. All the resources I can find seem to be about how to use selectors, and doesn't answer the question I have. What my client does is send a String to the server, the server makes it all caps, and then sends it back... very simple little test program.

Here are my questions
1) How on earth when reading from the SocketChannel do you know when to stop reading?
2) How do you handle cases where the data you are trying to read (or write) is bigger than the buffer?

I have more questions for when I get to the point of adding more clients, but I'll save those for later.

Client:


Server:
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you design a client server program, you will need to know when to stop reading.
to answer your first question: you can stop reading when you read an end tag. for example, if you are expecting an xml reply, you should stoping if you read </element1>.
to answer your second question, you just do it in a while loop to continue to read from the channel if the previous buffer is filled. make sure you clear the buffer before you read it again.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for write problem:


# while(buffer.hasRemaining())
# socketChannel.write(buffer);


you can change above to



Now you don't need to worry about the buffer size when writing.

for read problem:
I suggest that you use selector to notify whenever there is data to be read from the TCP Buffers. When reading you can specify a high buffer size depending upon maximum data that can be sent by the server. I guess that is always predictable in most cases.
Even if the data does not fit into your buffer the first time you read you can always get the leftover data from the TCP buffers on the second read after you receive a OP_READ key from the selector.

I hope this helps.
reply
    Bookmark Topic Watch Topic
  • New Topic