PracticalSocket.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __PRACTICALSOCKET_INCLUDED__
00021 #define __PRACTICALSOCKET_INCLUDED__
00022
00023 #include <string>
00024 #include <exception>
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;
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
00122 Socket(const Socket &sock);
00123 void operator=(const Socket &sock);
00124
00125 protected:
00126 int sockDesc;
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
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