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
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;
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
00125 Socket(const Socket &sock);
00126 void operator=(const Socket &sock);
00127
00128 protected:
00129 int sockDesc;
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
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