PracticalSocket.h

Go to the documentation of this file.
00001 /*
00002  *   C++ sockets on Unix and Windows
00003  *   Copyright (C) 2002
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with this program; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef __PRACTICALSOCKET_INCLUDED__
00021 #define __PRACTICALSOCKET_INCLUDED__
00022 
00023 #include <string>            // For string
00024 #include <exception>         // For exception class
00025 
00026 using namespace std;
00027 
00028 class SocketException : public exception {
00029 public:
00036   SocketException(const string &message, bool inclSysMsg = false) throw();
00037 
00041   ~SocketException() throw();
00042 
00047   const char *what() const throw();
00048 
00049 private:
00050   string userMessage;  // Exception message
00051 };
00052 
00056 class Socket {
00057 public:
00061   ~Socket();
00062 
00068   string getLocalAddress() throw(SocketException);
00069 
00075   unsigned short getLocalPort() throw(SocketException);
00076 
00083   void setLocalPort(unsigned short localPort) throw(SocketException);
00084 
00093   void setLocalAddressAndPort(const string &localAddress, 
00094     unsigned short localPort = 0) throw(SocketException);
00095 
00109   static void cleanUp() throw(SocketException);
00110 
00117   static unsigned short resolveService(const string &service,
00118                                        const string &protocol = "tcp");
00119 
00120 private:
00121   // Prevent the user from trying to use value semantics on this object
00122   Socket(const Socket &sock);
00123   void operator=(const Socket &sock);
00124 
00125 protected:
00126   int sockDesc;              // Socket descriptor
00127   Socket(int type, int protocol) throw(SocketException);
00128   Socket(int sockDesc);
00129 };
00130 
00134 class CommunicatingSocket : public Socket {
00135 public:
00144   void connect(const string &foreignAddress, unsigned short foreignPort)
00145     throw(SocketException);
00146 
00155   void send(const void *buffer, int bufferLen) throw(SocketException);
00156 
00165   int recv(void *buffer, int bufferLen) throw(SocketException);
00166 
00172   string getForeignAddress() throw(SocketException);
00173 
00179   unsigned short getForeignPort() throw(SocketException);
00180 
00181 protected:
00182   CommunicatingSocket(int type, int protocol) throw(SocketException);
00183   CommunicatingSocket(int newConnSD);
00184 };
00185 
00189 class TCPSocket : public CommunicatingSocket {
00190 public:
00195   TCPSocket() throw(SocketException);
00196 
00204   TCPSocket(const string &foreignAddress, unsigned short foreignPort) 
00205       throw(SocketException);
00206 
00207 private:
00208   // Access for TCPServerSocket::accept() connection creation
00209   friend class TCPServerSocket;
00210   TCPSocket(int newConnSD);
00211 };
00212 
00216 class TCPServerSocket : public Socket {
00217 public:
00227   TCPServerSocket(unsigned short localPort, int queueLen = 5) 
00228       throw(SocketException);
00229 
00239   TCPServerSocket(const string &localAddress, unsigned short localPort,
00240       int queueLen = 5) throw(SocketException);
00241 
00247   TCPSocket *accept() throw(SocketException);
00248 
00249 private:
00250   void setListen(int queueLen) throw(SocketException);
00251 };
00252 
00256 class UDPSocket : public CommunicatingSocket {
00257 public:
00262   UDPSocket() throw(SocketException);
00263 
00269   UDPSocket(unsigned short localPort) throw(SocketException);
00270 
00277   UDPSocket(const string &localAddress, unsigned short localPort) 
00278       throw(SocketException);
00279 
00285   void disconnect() throw(SocketException);
00286 
00297   void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
00298             unsigned short foreignPort) throw(SocketException);
00299 
00310   int recvFrom(void *buffer, int bufferLen, string &sourceAddress, 
00311                unsigned short &sourcePort) throw(SocketException);
00312 
00318   void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
00319 
00325   void joinGroup(const string &multicastGroup) throw(SocketException);
00326 
00332   void leaveGroup(const string &multicastGroup) throw(SocketException);
00333 
00334 private:
00335   void setBroadcast();
00336 };
00337 
00338 #endif

Generated on Thu Sep 17 23:14:16 2009 for CSL by  doxygen 1.5.8