home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OIP_Server.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  172 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1996
  3. // (c) 1982, 1985, 1986 Regents of the University of California.
  4. // All Rights Reserved
  5. // OIP_Server.cpp
  6.  
  7. /*
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  14.  *    endorse or promote products derived from this software
  15.  *    without specific prior written permission.
  16.  * 3. See OCL.INF for a detailed copyright notice.
  17.  *
  18.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  19.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE.
  29.  */
  30.  
  31. // $Header: W:/Projects/OCL/Source/rcs/OIP_Server.cpp 1.50 1996/08/11 23:49:19 B.STEIN Release $
  32.  
  33. #define __OCL_SOURCE__
  34.  
  35. #define OINCL_OSTRING
  36. #define OINCL_BASE
  37.  
  38. #include <ocl.hpp>
  39. #include <OIP_Server.hpp>
  40. #include <OMessage.hpp>
  41.  
  42.  
  43. OIP_Server::OIP_Server()
  44.   : port(0),
  45.     active(FALSE)
  46.   {}
  47.  
  48.   
  49. OIP_Server::~OIP_Server()
  50. {
  51.  if (active)
  52.     terminate();
  53. }
  54.  
  55.  
  56. PSZ OIP_Server::isOfType() const
  57. {
  58.  return("OIP_Server");
  59.  
  60.  
  61. OIP_Server& OIP_Server::init(USHORT _port)
  62. {
  63.  port = _port;
  64.  server.sysinit(1 | (1 << 8));             // TCP/IP Init
  65.  server.socket();                          // create Socket
  66.  server.Setin_port(port); // set Port
  67.  server.Set_sin_addr(INADDR_ANY);          // IP-Adresse
  68.  server.bind();                            // binden
  69.  server.listen();                          // Bereitschaft
  70.  active = TRUE;
  71.  return(*this);
  72. }
  73.  
  74.  
  75. OIP_Server& OIP_Server::terminate()
  76. {
  77.  client.close();
  78.  server.close();
  79.  server.sysfree();
  80.  active = FALSE;
  81.  return(*this);
  82. }
  83.  
  84.  
  85. OIP_Server& OIP_Server::waitForClient()
  86. {
  87.  server.accept(&client);
  88.  
  89.  if ((INT)client == -1)
  90.    throw OVioException(OIP::error(17), 1);
  91.  
  92.  return(*this);
  93. }
  94.   
  95.  
  96. OIP_Server& OIP_Server::connectMsg()
  97. {
  98.  OString  clientadress(20);
  99.  
  100.  client.Get_inet_addr(clientadress);
  101.  cout << OIP::error(18).getText()
  102.       << " "
  103.       << clientadress.getText()
  104.       << " ";
  105.  cout << OIP::error(19).getText()
  106.       << " "
  107.       << client.Getin_port()
  108.       << " ["
  109.       << (int)client
  110.       << "]"
  111.       << endl;  
  112.  return(*this);
  113. }
  114.  
  115.   
  116. OIP_Server& OIP_Server::disconnect()
  117. {
  118.  client.close();
  119.  return(*this);
  120. }
  121.  
  122.  
  123. OIP_Server& OIP_Server::disconnectMsg()
  124. {
  125.  OString  clientadress(20);
  126.  
  127.  client.Get_inet_addr(clientadress);
  128.  cout << OIP::error(20).getText()
  129.       << " "
  130.       << clientadress.getText()
  131.       << " ";
  132.  cout << OIP::error(19).getText()
  133.       << " "
  134.       << client.Getin_port()
  135.       << " ["
  136.       << (int)client
  137.       << "]"
  138.       << endl;  
  139.  return(*this);
  140. }
  141.  
  142.  
  143. OIP_Server& OIP_Server::send(PVOID data, ULONG size)
  144. {
  145.  client.send((PSZ)data, size);
  146.  return(*this);
  147. }
  148.  
  149.  
  150. OIP_Server& OIP_Server::sendSuccess()
  151. {
  152.  return(send("OK", 3));
  153. }
  154.    
  155.  
  156.  
  157. OIP_Server& OIP_Server::sendEnd()
  158. {
  159.  return(send("END", 4));
  160. }
  161.  
  162.  
  163.    
  164. OIP_Server& OIP_Server::recv(PVOID data, ULONG size)
  165. {
  166.  client.recv((PSZ)data, size);
  167.  return(*this);
  168. }
  169.  
  170. // end of source
  171.