Hey everybody,

I'm thinking about writing a chat server/client that can have multiple chat rooms. I'm thinking at the server end, each chat room gets a message queue for the incoming messages. Now to handle this, each message sent by the client must tell the server which channel this message belongs to. The question is: what's the best way to recv the data to the right message queue? I figure there are 2 approaches:

1) recieve the data into a buffer, check which queue it belongs to and then copy it to the queue from the buffer

This seems redundant because I'd rather just receive it to the right queue.

2) recieve the data with MSG_PEEK to take a look at the first couple of bytes and then recieve it to the right buffer.

This is also redundant, and I've read that any peeking should be avoided.

Do I have to choose between the lesser of two evils here?
If so, what's the best solution

Thanks
--Chorus
Posted on 2002-09-04 19:10:17 by chorus
the best way is generate identifier for each message, telling to which channel it belongs. Then the server will put it to the right channel buffer. The best, that it requires only one thread per connection, if you are allowing more than one channel per connection.
Posted on 2002-09-07 09:39:00 by kahuna
Originally posted by chorus
1) recieve the data into a buffer, check which queue it belongs to and then copy it to the queue from the buffer


That's okay but keep in mind that the buffer doesn't have to contain the full data exactly like you want it. The buffer may contain part of the message, one full message and the first half of the second message, and so on.

This seems redundant because I'd rather just receive it to the right queue.


I'm sure copying the data wouldn't affect the program speed :). Besides, as I said above you need to cut out the pieces of each message from the main buffer and reconstruct them in the right queue..

2) recieve the data with MSG_PEEK to take a look at the first couple of bytes and then recieve it to the right buffer. This is also redundant, and I've read that any peeking should be avoided.


Article about avoiding peeking: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q192599&
I don't think peeking is a good idea since it's inefficient. You will only read small portions of the buffer (each message is received seperately) instead of reading it in blocks in the main buffer.

Thomas
Posted on 2002-09-08 04:08:49 by Thomas
You may wanna peek at the LinkedList chat example I posted ... I figure out what queue owns the data, and create a new linked object for the data then recieve it into there, so the packet buffers are linked lists per client in that example. Although my example code isn't wonderful, the logic behind using dynamic arrays is sound, and they're not hard to implement.
Posted on 2002-09-10 02:41:23 by Homer