/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          server_test.cpp  -  description
                             -------------------
    begin                : Wed Apr 20, 2005
    copyright            : (C) 2005 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at
   your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <iostream>

#include <sptk3/CSocket.h>
#include <sptk3/CException.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef _WIN32
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#endif


int main ( int argc, int argv[] )
{
   std::cout << "running....\n";

   try {
      // Create the socket
      CSocket server;
      server.port(3000);
      server.host("localhost");

      int clientSocketFD;
      struct sockaddr_in clientInfo;

      server.listen();
      server.accept(clientSocketFD,clientInfo);

      CSocket new_sock;
      new_sock.attach(clientSocketFD);

      try {
         std::string data;

         std::cout << "Sending: Test SPTK server 1.00\n";
         new_sock << "Test SPTK server 1.00\n";

         std::cout << "Receving (strings): ";

         do {
            new_sock >> data;
            std::cout << data.c_str() << "\n";
         } while (data != "EOD");

         std::cout << "Sending: confirmation\n";
         new_sock << "Data accepted\n";

         // End of session
         try {
            new_sock >> data;
         }
         catch(...) {}

         server.close();
      }
      catch (std::exception& e) {}
   }
   catch (std::exception& e) {
      std::cout << "Exception was caught: " << e.what() << "\nExiting.\n";
   }

   std::cout << "Server session closed" << std::endl;

   return 0;
}
