home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Samples / Sockets / Source / SOCKS.CPP < prev    next >
C/C++ Source or Header  |  1997-01-01  |  5KB  |  199 lines

  1. // OCL - OS/2 Class Library (Samples)
  2. // (c) Cubus 1996
  3. // (c) 1982, 1985, 1986 Regents of the University of California.
  4. // (c) Raoul Gema 1996
  5. // All Rights Reserved
  6. // SOCKS.CPP
  7.  
  8. /*
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  15.  *    endorse or promote products derived from this software
  16.  *    without specific prior written permission.
  17.  * 3. See OCL.INF for a detailed copyright notice.
  18.  *
  19.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31.  
  32. // $Header: W:/Projects/OCL/Samples/Sockets/Source/rcs/SOCKS.CPP 1.50 1996/08/11 23:48:54 B.STEIN Release $
  33.  
  34. #include <ocl.hpp>
  35. #include <OIP_Server.hpp>
  36. #include <OMessage.hpp>
  37. #include <OXcptVio.hpp>
  38.  
  39. #define LOGO        "CT.NET++ Mini-Server"
  40. #define FILENAME    "DEMO.TXT"
  41.  
  42.  
  43. class SystemHandler
  44.   : public OXcptVio
  45. {
  46.  public:
  47.      SystemHandler   ();
  48.  
  49.    virtual
  50.      ~SystemHandler  ();
  51.  
  52.    virtual  
  53.       PSZ isOfType   () const; 
  54.    BOOL killTrace    ();
  55.    BOOL trapTrace    ();
  56. };
  57.  
  58.  
  59.  
  60. int main(int argc, char* argv[])
  61. {
  62.  OXCPTRECORD   exceptionRecord;
  63.  SystemHandler except;
  64.  OIP_Server    server;
  65.  int           ok;
  66.  
  67.  cout << LOGO
  68.       << endl
  69.       << OIP::Version()
  70.       << endl;
  71.  
  72.  
  73.  if (argc != 2)
  74.   {
  75.    cout << "Usage: SOCKS Portnummer" << endl;
  76.    exit(1);
  77.   }
  78.  
  79.  except.setHandler(exceptionRecord); // set system exception handler
  80.  
  81.  try
  82.   {
  83.    server.init((USHORT)atoi(argv[1]));
  84.  
  85.    for (;;)
  86.     {
  87.      server.waitForClient().connectMsg();
  88.  
  89.      try
  90.       {
  91.        char data[1024];
  92.        sprintf(data, "%s\n%s", LOGO, OIP::Version().getText());
  93.        server.send(data, strlen(data) + 1);  // Logo senden
  94.        server.recv(data, sizeof(data));      // empfangen
  95.        cout << "Received: "
  96.             << data
  97.             << endl; 
  98.  
  99.        switch (data[0]) {
  100.          case '+': {
  101.            ofstream file(FILENAME,    ios::out | ios::ate);
  102.            if (file) {
  103.              file << (char*)(data + 2) << endl;
  104.              strcpy(data, "OK"); }
  105.            else
  106.              strcpy(data, "Internal error saving data.");
  107.           file.close();
  108.           break; }
  109.  
  110.         case '=': {
  111.           ifstream file(FILENAME, ios::in | ios::nocreate);
  112.           if (!file)
  113.             sprintf(data, "No information available.");
  114.           else
  115.             file.get(data, sizeof(data), '\0');
  116.           file.close();
  117.           break; }
  118.  
  119.         case '?': {
  120.           ifstream file(FILENAME, ios::in | ios::nocreate);
  121.           if (!file)
  122.             sprintf(data, "No information available.");
  123.           else {
  124.             char line[256];
  125.             int found = 0;
  126.             while (!found) {
  127.               file.get(line, sizeof(line), '\n');
  128.               if (file.eof()) break;
  129.               file.get();
  130.               if (!strcmp(data + 2, line))
  131.               found++; }
  132.             strcpy(data, (found) ?    "OK" : "Requested string not found."); }
  133.         file.close();
  134.           break; }
  135.  
  136.         default:
  137.          sprintf(data, "Unknown command."); }
  138.  
  139.        server.send(data, strlen(data) + 1);    //* Antwort senden
  140.        cout << "Sent    : " 
  141.             << data
  142.             << endl; 
  143.        server.disconnect().disconnectMsg();
  144.       }
  145.  
  146.      catch (OException& e)
  147.       {
  148.        e.viewError();
  149.       }
  150.  
  151.      ok = 0;
  152.     }
  153.   }
  154.  
  155.  catch (OException& e)
  156.   {
  157.    e.viewError();
  158.    ok = -1;
  159.   }
  160.  
  161.  catch (...)
  162.   {
  163.    printf(OIP::error(9));
  164.    ok = -1;
  165.   }
  166.  
  167.  except.unsetHandler();           // release system exception handler
  168.  
  169.  return ok;
  170. }
  171.  
  172.  
  173.  
  174. SystemHandler::SystemHandler()
  175.  : OXcptVio(FALSE)
  176.  {}
  177.  
  178. SystemHandler::~SystemHandler()
  179.  {}
  180.  
  181. PSZ SystemHandler::isOfType   () const 
  182. {
  183.  return("SystemHandler");
  184. }
  185.  
  186. BOOL SystemHandler::killTrace()
  187. {
  188.  cout << endl << "Terminating " << LOGO << endl;
  189.  return(FALSE); 
  190. }
  191.  
  192.  
  193. BOOL SystemHandler::trapTrace()
  194. {
  195.  return(FALSE);  // show exception message and abort
  196. }
  197.  
  198. // end of source
  199.