Chatting with a server

James Miller james at aatch.net
Tue Feb 14 15:10:11 PST 2012


On 14 February 2012 18:00, Pedro Lacerda <pslacerda at gmail.com> wrote:
> How would you chat with a server? This is my naive attempt:
>
> class Client {
>    Socket socket;
>
>   Token fetch() {
>     ubyte[1024] buffer;  // if more space is needed...
>      socket.receive(buffer);
>      return decode(buffer);
>   }
>
>   void send(Token tkn) {
>    ubyte[] buffer = encode(tkn);
>    socket.send(buffer);
>   }
> }
>
> It can send and fetch data (encoded tokens) to/from server. But frequently I
> see network clients using a connection pool or an event loop. I descarted
> event loops because they looks hard to implement (and libev docs don't let
> me anywhere).
>
> Do you can guide me on this trouble?
>
> Pedro Lacerda
>

With a client, your way is probably fine, assuming that you have a
reasonable amount of control over  the data flow. The advantage of
event-based systems is that you can be waiting for data from the
server and for user input at the same time, meaning that a slow server
doesn't break your client or render it unusable. If the code works for
you, then its probably fine.

James Miller


More information about the Digitalmars-d-learn mailing list