Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

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 
00031 class SocketException : public exception {
00032 public:
00039   SocketException(const string &message, bool inclSysMsg = false) throw();
00040 
00044   ~SocketException() throw();
00045 
00050   const char *what() const throw();
00051 
00052 private:
00053   string userMessage;  // Exception message
00054 };
00055 
00059 class Socket {
00060 public:
00064   ~Socket();
00065 
00071   string getLocalAddress() throw(SocketException);
00072 
00078   unsigned short getLocalPort() throw(SocketException);
00079 
00086   void setLocalPort(unsigned short localPort) throw(SocketException);
00087 
00096   void setLocalAddressAndPort(const string &localAddress, 
00097     unsigned short localPort = 0) throw(SocketException);
00098 
00112   static void cleanUp() throw(SocketException);
00113 
00120   static unsigned short resolveService(const string &service,
00121                                        const string &protocol = "tcp");
00122 
00123 private:
00124   // Prevent the user from trying to use value semantics on this object
00125   Socket(const Socket &sock);
00126   void operator=(const Socket &sock);
00127 
00128 protected:
00129   int sockDesc;              // Socket descriptor
00130   Socket(int type, int protocol) throw(SocketException);
00131   Socket(int sockDesc);
00132 };
00133 
00137 class CommunicatingSocket : public Socket {
00138 public:
00146   void connect(const string &foreignAddress, unsigned short foreignPort)
00147     throw(SocketException);
00148 
00156   void send(const void *buffer, int bufferLen) throw(SocketException);
00157 
00166   int recv(void *buffer, int bufferLen) throw(SocketException);
00167 
00173   string getForeignAddress() throw(SocketException);
00174 
00180   unsigned short getForeignPort() throw(SocketException);
00181 
00182 protected:
00183   CommunicatingSocket(int type, int protocol) throw(SocketException);
00184   CommunicatingSocket(int newConnSD);
00185 };
00186 
00190 class TCPSocket : public CommunicatingSocket {
00191 public:
00196   TCPSocket() throw(SocketException);
00197 
00205   TCPSocket(const string &foreignAddress, unsigned short foreignPort) 
00206       throw(SocketException);
00207 
00208 private:
00209   // Access for TCPServerSocket::accept() connection creation
00210   friend class TCPServerSocket;
00211   TCPSocket(int newConnSD);
00212 };
00213 
00217 class TCPServerSocket : public Socket {
00218 public:
00228   TCPServerSocket(unsigned short localPort, int queueLen = 5) 
00229       throw(SocketException);
00230 
00240   TCPServerSocket(const string &localAddress, unsigned short localPort,
00241       int queueLen = 5) throw(SocketException);
00242 
00248   TCPSocket *accept() throw(SocketException);
00249 
00250 private:
00251   void setListen(int queueLen) throw(SocketException);
00252 };
00253 
00257 class UDPSocket : public CommunicatingSocket {
00258 public:
00263   UDPSocket() throw(SocketException);
00264 
00270   UDPSocket(unsigned short localPort) throw(SocketException);
00271 
00278   UDPSocket(const string &localAddress, unsigned short localPort) 
00279       throw(SocketException);
00280 
00286   void disconnect() throw(SocketException);
00287 
00298   void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
00299             unsigned short foreignPort) throw(SocketException);
00300 
00311   int recvFrom(void *buffer, int bufferLen, string &sourceAddress, 
00312                unsigned short &sourcePort) throw(SocketException);
00313 
00319   void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
00320 
00326   void joinGroup(const string &multicastGroup) throw(SocketException);
00327 
00333   void leaveGroup(const string &multicastGroup) throw(SocketException);
00334 
00335 private:
00336   void setBroadcast();
00337 };
00338 
00339 #endif

Generated on Fri Mar 19 09:30:41 2010 for ImpalaSrc by  doxygen 1.5.1