PREV UP NEXT C++ socket classes for OS/2

Chapter 11: Echo Class

The echo class implements RFC 862. An echo object, as a client, will get back what ever data it sends to an echo server. Similarly, an echo object, as a server, will echo back the data it receives from its client.

The echo class is derived from protocol class, and uses echo::echobuf as its stream buffer. echo::echobuf is in turn is derived from protocol::protcolbuf.

In what follows,

echo e (pname)
constructs the echo object, e with pname as its transport protocol name.
echo::operator -> ()
an echo object is a smart pointer for the underlying echobuf.

11.0.1: tsecho.C


// echo server. Serves clients at port 4000.

#include <echo.h>

#include <stdlib.h>



int main ()

{

  becho server (protocol::tcp);

  server->serve_clients (4000);

  return 1;

}

11.0.2: tcecho.C


// echo client. Sends "mary had a litte lamb" to the server

#include <echo.h>

#include <stdlib.h>



int main ()

{

  becho e(protocol::tcp);



  e->connect ("kelvin.seas.virginia.edu", 4000);



  cout << e->rfc_name () << ' ' << e->rfc_doc () << endl;



  e << "mary had a little lamb\r\n" << flush;



  char buf [256];

  e.getline (buf, 255);



  cout << "got back: " << buf << endl;

  return 0;

}