www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Chatting with a server

--000e0ce0391e9fff1404b8e579a6
Content-Type: text/plain; charset=UTF-8

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

--000e0ce0391e9fff1404b8e579a6
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>How would you chat with a server?=C2=A0This is my naive attempt:</div>=
<div><br></div><div>class Client {</div><div>=C2=A0 =C2=A0Socket socket;</d=
iv><div>=C2=A0 =C2=A0</div><div>=C2=A0 Token fetch() {</div><div>=C2=A0 =C2=
=A0=C2=A0ubyte[1024] buffer; =C2=A0// if more space is needed...</div>

<div>=C2=A0 =C2=A0 =C2=A0socket.receive(buffer);</div><div>=C2=A0 =C2=A0 =
=C2=A0return decode(buffer);</div><div>=C2=A0 }</div><div>=C2=A0</div><div>=
=C2=A0 void send(Token tkn) {</div><div>=C2=A0 =C2=A0ubyte[] buffer =3D enc=
ode(tkn);</div><div>=C2=A0 =C2=A0socket.send(buffer);</div><div>
=C2=A0 }</div>
<div>}</div><div><div><br></div><div>It can send and fetch data (encoded to=
kens) to/from server. But frequently I see network clients using a connecti=
on pool or an event loop. I descarted event loops because they looks hard t=
o implement (and libev docs don&#39;t let me anywhere).</div>

<div><br></div><div>Do you can guide me on this trouble?</div><div><br></di=
v>Pedro Lacerda<br><br>
</div>

--000e0ce0391e9fff1404b8e579a6--
Feb 13 2012