00001 /* 00002 Copyright (c) 2000,2001, Jelle Kok, University of Amsterdam 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 00008 1. Redistributions of source code must retain the above copyright notice, this 00009 list of conditions and the following disclaimer. 00010 00011 2. Redistributions in binary form must reproduce the above copyright notice, 00012 this list of conditions and the following disclaimer in the documentation 00013 and/or other materials provided with the distribution. 00014 00015 3. Neither the name of the University of Amsterdam nor the names of its 00016 contributors may be used to endorse or promote products derived from this 00017 software without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00043 #ifndef _CONNECTION_ 00044 #define _CONNECTION_ 00045 00046 #include <stdio.h> // printf, FILE 00047 #include <ostream.h> // ostream 00048 #include <netdb.h> // sockaddr_in 00049 #include <arpa/inet.h> // sockaddr_in 00050 00052 typedef struct _socket{ 00053 int socketfd ; 00054 struct sockaddr_in serv_addr ; 00055 } Socket ; 00056 00057 /********************** CONNECTION ********************************************/ 00058 00064 class Connection { 00065 00066 Socket m_sock; 00067 int m_iMaxMsgSize; 00068 public: 00069 00070 // constructors 00071 Connection ( ); 00072 Connection ( const char *hostname, int port, int iSize ); 00073 ~Connection ( ); 00074 00075 // methods that deal with the connection itself 00076 bool connect ( const char *host, int port ); 00077 void disconnect ( void ); 00078 bool isConnected ( void ) const; 00079 00080 // methods that deal with the communication over the connection 00081 int message_loop ( FILE *in, FILE *out ); 00082 int receiveMessage ( char *msg, int maxsize ); 00083 bool sendMessage ( const char *msg ); 00084 00085 void show ( ostream os ); 00086 00087 }; 00088 00089 #endif