• 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

Using Buffers in a non-blocking server

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have what I think is a very simple question. I'm creating a server, that processes Strings that are sent to it. I have to keep it very simple for several reasons, including backwards compatibility. For example, if client1 wants to send client8 a message, it might send the server a String such as "send" + DELIM + "client8" + DELIM + "Hi client8!" + EOS". I''ve recently graduated with my CS degree, however in my networks class we only programmed using normal Sockets so this non-blocking stuff is fairly new to me.

I made a successful test client/server that takes what the client sends it and echos it back in all caps. However, that didn't use buffers. I just read from the client until it got the String termination character. I need it to be able to receive part of a String, and then stick it somewhere (I'm thinking I should use a Stringbuffer). Then maybe it reads from some other clients before coming back to the original client and reading in the rest of the message. My question is: When it started to read the 2nd half of the message, what is the best way to figure out where the correct buffer is? What I initially did was make a Connection class that stores a SocketChannel and a Stringbuffer, but then I had to iterate over a collection of Connections and check every single one. Surely there is some way to do it without iteration.

Something else I had considered was using a HashMap<StringBuffer, SocketChannel> but there is no guarantee that two SocketChannels will not hash to the same value. As far as I can tell, keys in a hashmap have to be unique.

I'm just wondering if anyone here has any experience with how to buffer received messages when using non-blocking reads.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like your problem isn't with buffers, it is with choosing a data structure.
I'll move this post to a more appropriate forum.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Sounds like you're trying to store Data vs a SocketChannel but are worried about uniqueness. How about designing a DTO that wraps say (Id, StringBuffer) and put that DTO in a HashMap with the corresponding SocketChannel. The data will always be unique (due to the Id).

Disclaimer - I'm not a non-blocking I/O expert, there are likely better solutions out there!
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Harris wrote:
Something else I had considered was using a HashMap<StringBuffer, SocketChannel> but there is no guarantee that two SocketChannels will not hash to the same value. As far as I can tell, keys in a hashmap have to be unique.


I assume you meant HashMap<SocketChannel, StringBuffer>.
For all practical purposes you can assume that the hashcode of 2 socketchannels will not be equal because this class does not override equals() & hashcode() and the default implementation is to return the memory address as the hashcode and check equality based on the memory address.

In order to read from the channel, you must be using a ByteBuffer, it is better to use the ByteBuffer for the entire reading and finally convert it to a string.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic