Listing 6: Sample server socket application

// file testss.cpp
#include "stdhead.h"
#include "flsocket.h"
#include "ssocket.h"

int main(void)
{
   string h1;
   bool done = FALSE;

   ssocket s1(10010);

   if (!s1.is_init())
      {
      cout << "server init error\n";
      return(0);
      }

   while(!done)
      {
      cout << "waiting for client connection...";

      if (!s1.server_wait())
         {
         cout << "\nclient connection error\n";
         return(0);
         }

      cout << "\nclient connection detected\n";

      if (s1.recv_data(h1) == 0)
         cout << "error receiving client data\n";
      else
         {
         cout << "received:" << h1 << ",l=" << h1.length() 
              << '\n';

         if (h1 == "end")
            done = TRUE;

         h1 = "this is the server reply string";

         if (s1.send_data(h1) == 0)
            cout << "errror sending client data\n";
         else
            cout << "send complete\n";
         }

      if (!s1.close_client())
         cout << "error closing client socket\n";
      }

   if (!s1.close_all())
      cout << "error closing sockets\n";
   else
      cout << "termination requested\n";

   return(0);
}
— End of Listing —