home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OIP_Client.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  183 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_Client.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_Client.cpp 1.50 1996/08/11 23:49:18 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_Client.hpp>
  40. #include <OMessage.hpp>
  41.  
  42.  
  43. OIP_Client::OIP_Client()
  44.   : port(0),
  45.     active(FALSE)
  46.   {}  
  47.  
  48.  
  49. OIP_Client::~OIP_Client()
  50. {
  51.  if (active)
  52.    terminate();
  53. }
  54.  
  55. PSZ OIP_Client::isOfType() const
  56. {
  57.  return("OIP_Client");
  58.  
  59.  
  60. OIP_Client& OIP_Client::init()
  61. {
  62.  client.sysinit(1 | (1 << 8));
  63.  client.socket();
  64.  return(*this);
  65. }
  66.  
  67.  
  68. OIP_Client& OIP_Client::terminate()
  69. {
  70.  client.close();
  71.  client.sysfree();
  72.  return(*this);
  73. }  
  74.  
  75.  
  76. OIP_Client& OIP_Client::connect(PSZ name, USHORT _port)
  77. {
  78.  BOOL      nameIsIP = FALSE;
  79.  OString   address(20); 
  80.  OString   hname(CCHMAXPATH);
  81.  
  82.  port = _port;
  83.  
  84.  try  // assume name to be a host name
  85.   { 
  86.    client.Gethostbyname(name);
  87.    client.Set_sin_addr();
  88.    client.Setin_port(port);
  89.    client.Get_inet_addr(address);
  90.    servername    << name;
  91.    serveraddress << address;   
  92.   }
  93.  catch(OIP_Socket::OIP_SocketException ex)
  94.   {
  95.    if (ex.etyp == OIP_Socket::OIP_SocketException::egethostbyname)
  96.      nameIsIP = TRUE;
  97.    else
  98.      throw ex;
  99.   }
  100.  
  101.  if (nameIsIP) // assume name to be IP address
  102.   {
  103.    try
  104.     {
  105.      client.Set_sin_addr(name);
  106.      client.Setin_port(port);
  107.      client.Gethostname(hname.getText(), CCHMAXPATH); 
  108.      servername    << hname;
  109.      serveraddress << name;   
  110.     }
  111.    catch(OIP_Socket::OIP_SocketException ex)
  112.     {
  113.      ex.etyp = OIP_Socket::OIP_SocketException::egethostbyaddr;
  114.      throw ex;
  115.     }
  116.   }
  117.   
  118.  client.connect();
  119.  active = TRUE;
  120.  return(*this);
  121. }
  122.   
  123. OIP_Client& OIP_Client::connectMsg()
  124. {
  125.  cout << OIP::error(21).getText();
  126.  cout << " "
  127.       << servername.getText()
  128.       << " "
  129.       << serveraddress.getText()
  130.       << " Port: "
  131.       << client.Getin_port()
  132.       << endl;
  133.  return(*this);
  134. }
  135.  
  136.  
  137. OIP_Client& OIP_Client::disconnect()
  138. {
  139.  return(terminate());
  140. }
  141.  
  142.  
  143. OIP_Client& OIP_Client::disconnectMsg()
  144. {
  145.  cout << OIP::error(23).getText();
  146.  cout << " "
  147.       << servername.getText()
  148.       << " "
  149.       << serveraddress.getText()
  150.       << endl;
  151.  
  152.  return(*this);
  153. }
  154.  
  155.  
  156. OIP_Client& OIP_Client::send(PVOID data, ULONG size)
  157. {
  158.  client.send((PSZ)data, size);
  159.  return(*this);
  160. }
  161.  
  162. OIP_Client& OIP_Client::sendSuccess()
  163. {
  164.  return(send("OK", 3));
  165. }
  166.  
  167.    
  168. OIP_Client& OIP_Client::sendEnd()
  169. {
  170.  return(send("END", 4));
  171. }
  172.    
  173.  
  174. OIP_Client& OIP_Client::recv(PVOID data, ULONG size)
  175. {
  176.  client.recv((PSZ)data, size);
  177.  return(*this);
  178. }
  179.  
  180.  
  181. // end of source
  182.