00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef __PRACTICALSOCKET_INCLUDED__
00053 #define __PRACTICALSOCKET_INCLUDED__
00054
00055 #include <string>
00056 #include <exception>
00057
00058 using namespace std;
00059
00063 class SocketException : public exception {
00064 public:
00071 SocketException(const string &message, bool inclSysMsg = false) throw();
00072
00076 ~SocketException() throw();
00077
00082 const char *what() const throw();
00083
00084 private:
00085 string userMessage;
00086 };
00087
00091 class Socket {
00092 public:
00096 ~Socket();
00097
00103 string getLocalAddress() throw(SocketException);
00104
00110 unsigned short getLocalPort() throw(SocketException);
00111
00118 void setLocalPort(unsigned short localPort) throw(SocketException);
00119
00128 void setLocalAddressAndPort(const string &localAddress,
00129 unsigned short localPort = 0) throw(SocketException);
00130
00144 static void cleanUp() throw(SocketException);
00145
00152 static unsigned short resolveService(const string &service,
00153 const string &protocol = "tcp");
00154
00155 private:
00156
00157 Socket(const Socket &sock);
00158 void operator=(const Socket &sock);
00159
00160 protected:
00161 int sockDesc;
00162 Socket(int type, int protocol) throw(SocketException);
00163 Socket(int sockDesc);
00164 };
00165
00169 class CommunicatingSocket : public Socket {
00170 public:
00178 void connect(const string &foreignAddress, unsigned short foreignPort)
00179 throw(SocketException);
00180
00188 void send(const void *buffer, int bufferLen) throw(SocketException);
00189
00198 int recv(void *buffer, int bufferLen) throw(SocketException);
00199
00205 string getForeignAddress() throw(SocketException);
00206
00212 unsigned short getForeignPort() throw(SocketException);
00213
00214 protected:
00215 CommunicatingSocket(int type, int protocol) throw(SocketException);
00216 CommunicatingSocket(int newConnSD);
00217 };
00218
00222 class TCPSocket : public CommunicatingSocket {
00223 public:
00228 TCPSocket() throw(SocketException);
00229
00237 TCPSocket(const string &foreignAddress, unsigned short foreignPort)
00238 throw(SocketException);
00239
00240 private:
00241
00242 friend class TCPServerSocket;
00243 TCPSocket(int newConnSD);
00244 };
00245
00249 class TCPServerSocket : public Socket {
00250 public:
00260 TCPServerSocket(unsigned short localPort, int queueLen = 5)
00261 throw(SocketException);
00262
00272 TCPServerSocket(const string &localAddress, unsigned short localPort,
00273 int queueLen = 5) throw(SocketException);
00274
00280 TCPSocket *accept() throw(SocketException);
00281
00282 private:
00283 void setListen(int queueLen) throw(SocketException);
00284 };
00285
00289 class UDPSocket : public CommunicatingSocket {
00290 public:
00295 UDPSocket() throw(SocketException);
00296
00302 UDPSocket(unsigned short localPort) throw(SocketException);
00303
00310 UDPSocket(const string &localAddress, unsigned short localPort)
00311 throw(SocketException);
00312
00318 void disconnect() throw(SocketException);
00319
00330 void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
00331 unsigned short foreignPort) throw(SocketException);
00332
00343 int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
00344 unsigned short &sourcePort) throw(SocketException);
00345
00351 void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
00352
00358 void joinGroup(const string &multicastGroup) throw(SocketException);
00359
00365 void leaveGroup(const string &multicastGroup) throw(SocketException);
00366
00367 private:
00368 void setBroadcast();
00369 };
00370
00371 #endif