• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Buffers in a non-blocking server  RSS feed

 
Scott Harris
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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!
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!