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.