sock/zz_socket.h

Go to the documentation of this file.
00001 /* libzzsock
00002  *
00003  * This program is distributed under the GNU General Public License, version 2.
00004  * A copy of this license is included with this source.
00005  *
00006  * Copyright 2000-2006, Toni Thomsson <toni@tonjac.org> 
00007 */
00008 
00009 #ifndef zz_socket_h
00010 #define zz_socket_h
00011 
00012 #ifdef _WIN32
00013         #include <winsock2.h>
00014 #else
00015         #include <sys/types.h>
00016         #include <sys/socket.h> 
00017         #include <netinet/in.h>
00018         #include <arpa/inet.h>
00019         #include <netdb.h>
00020         #include <unistd.h>
00021         #include <errno.h>
00022         typedef int SOCKET;
00023         #define INVALID_SOCKET -1
00024         #define SOCKET_ERROR -1
00025 #endif
00026 
00027 #include <zz_string.h>
00028 #include <zz_exception.h>
00029 #include <zz_sock_exception.h>
00030 #include <zz_export.h>
00031 
00032 /*
00033         Description:
00034         TCP Socket
00035 
00036         Author: 
00037         Toni Thomsson, toni@tonjac.org
00038 
00039         Library:
00040         libzzsock.a (HPUX) libzzsock.lib (win32)
00041 
00042         Platform:
00043         HPUX, win32
00044 */
00045 class ZZ_API CzzSocket
00046 {
00047 public:
00048 
00049         /*
00050                 Description: 
00051                 Skapar en socket
00052         
00053                 Remarks: 
00054                 Kopplar upp som en server
00055 
00056                 Throws:
00057                 CzzSockException
00058         */
00059         CzzSocket( const char* port,    // IN, port
00060                                 int que                         // IN, antal inkommande anrop som köas
00061                                 );
00062 
00063         /*
00064                 Remarks: 
00065                 Kopplar upp som en klient
00066         */
00067         CzzSocket( const char* host,                    // IN, host
00068                                 const char* port,                       // IN, port
00069                                 unsigned short s_timeout,       // IN, send timeout
00070                                 unsigned short r_timeout        // IN, receive timeout
00071                                 );
00072 
00073         /*
00074                 Remarks: 
00075                 Skapar ett socket-objekt från ett befintligt handtag
00076         */
00077         CzzSocket( SOCKET socket,               // IN, socket handtag 
00078                                 const char* host        // IN, host
00079                                 );
00080 
00081         /*
00082                 Remarks: 
00083                 Kopieringskonstruktor
00084         */
00085         CzzSocket( CzzSocket& obj 
00086                                 );
00087         /*
00088                 Remarks:
00089                 Defaultkonstruktor: kopplar inte upp, anropa ConnectAsClient eller ConnectAsServer
00090         */
00091         CzzSocket( void );
00092         
00093         virtual ~CzzSocket();
00094 
00095         /*
00096                 Description: 
00097                 Lyssnar efter inkommande anrop
00098 
00099                 Returns:
00100                 Ny socket för snack med anropande klient
00101 
00102                 Throws:
00103                 CzzSockException
00104         */
00105         CzzSocket* Accept( void );
00106 
00107         /*
00108                 Description: 
00109                 Läser data från socket
00110 
00111                 Returns:
00112                 Antal lästa bytes
00113 
00114                 Throws:
00115                 CzzSockException
00116         */
00117         int Receive( char* receiveBuffer,       // UT, data-buffert
00118                                         int bufferSize,         // IN, storlek på buffert
00119                                         int bytesAquired,       // IN, antal bytes att läsa
00120                                         int flags                       // IN, flaggor, se recv()
00121                                         );
00122 
00123         /*
00124                 Description: 
00125                 Skickar data via socket
00126 
00127                 Returns:
00128                 Antal skickade bytes
00129 
00130                 Throws:
00131                 CzzSockException
00132         */
00133         int Send( char* sendBuffer, // IN, databuffert
00134                                 int bufferSize, // IN, storlek på buffert
00135                                 int flags               // IN, flaggor, se send()
00136                                 );
00137 
00138         /*
00139                 Description: 
00140                 Kopplar upp socket som en server
00141 
00142                 Throws:
00143                 CzzSockException
00144         */
00145         void ConnectAsServer( void );
00146 
00147         /*
00148                 Description: 
00149                 Kopplar upp socket som en klient
00150 
00151                 Throws:
00152                 CzzSockException
00153         */
00154         void ConnectAsClient( void );
00155         
00156         /*
00157                 Description: 
00158                 Kopplar ner socket
00159 
00160                 Throws:
00161                 CzzSockException
00162         */
00163         void Disconnect( void );
00164 
00165         // Description: Socket-handtag
00166         SOCKET GetSocket( void ) { return m_Socket; };
00167         
00168         // Description: Host-namn
00169         CzzString GetHost( void ) { return m_Host; };
00170 
00171         // Description: Port
00172         CzzString GetPort( void ) { return m_Port; };
00173 
00174         // Description: Antal som läggs i kö
00175         unsigned int GetQue( void ) { return m_Que; };
00176 
00177         // Description: Send timeout
00178         unsigned short GetSendTimeout( void ) { return m_SendTimeout; };
00179 
00180         // Description: Receive timeout
00181         unsigned short GetReceiveTimeout( void ) { return m_ReceiveTimeout; };
00182 
00183         // Description: Sätter hostnamn
00184         void SetHost( const CzzString& host ) { m_Host = host; };
00185 
00186         // Description: Sätter port
00187         void SetPort( const CzzString& port ) { m_Port = port; };
00188 
00189         // Description: Sätter kö antal
00190         void SetQue( unsigned int que ) { m_Que = que; };
00191 
00192         // Description: Sätter send timeout
00193         void SetSendTimeout( unsigned short to ) { m_SendTimeout = to; };
00194 
00195         // Description: Sätter receive timeout
00196         void SetReceiveTimeout( unsigned short to ) { m_ReceiveTimeout = to; };
00197 
00198         // Description: Aktuell host
00199         CzzString GetCurrentHost( void );
00200 
00201         void setNonBlocking( bool nonblock );
00202         
00203         /*
00204                 Description: 
00205                 Initierar socket API
00206 
00207                 Remarks:
00208                 Behövs endast anropas under win32. Skall anropas innan man
00209                 skapar några CzzSocket-objekt
00210 
00211                 Throws:
00212                 CzzSockException
00213         */
00214         static void Init( void );
00215 
00216         /*
00217                 Description: 
00218                 Städar upp socket API
00219 
00220                 Remarks:
00221                 Behövs endast anropas under win32
00222         */
00223         static void Exit( void );
00224 
00225 protected:
00226 
00227         SOCKET m_Socket;
00228         CzzString m_Host;
00229         CzzString m_Port;
00230         unsigned int m_Que;
00231         unsigned short m_SendTimeout;
00232         unsigned short m_ReceiveTimeout;
00233 
00234         bool isDataAvailable( SOCKET sock );
00235 
00236 };
00237 
00238 #endif // zz_socket_h
00239 
00240 

Generated on Thu Sep 21 21:45:42 2006 for tonjac.org MINI/C++ library by  doxygen 1.4.6-NO