Open Ephys GUI
 All Classes Functions Variables
PracticalSocket.h
1 /*
2  * C++ sockets on Unix and Windows
3  * Copyright (C) 2002
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef __PRACTICALSOCKET_INCLUDED__
21 #define __PRACTICALSOCKET_INCLUDED__
22 
23 #include <string> // For string
24 #include <cstring> // For memset
25 #include <exception> // For exception class
26 #include <stdlib.h> // For atoi
27 
28 using namespace std;
29 
33 class SocketException : public exception {
34 public:
41  SocketException(const string &message, bool inclSysMsg = false) throw();
42 
46  ~SocketException() throw();
47 
52  const char *what() const throw();
53 
54 private:
55  string userMessage; // Exception message
56 };
57 
61 class Socket {
62 public:
66  ~Socket();
67 
73  string getLocalAddress() throw(SocketException);
74 
80  unsigned short getLocalPort() throw(SocketException);
81 
88  void setLocalPort(unsigned short localPort) throw(SocketException);
89 
98  void setLocalAddressAndPort(const string &localAddress,
99  unsigned short localPort = 0) throw(SocketException);
100 
114  static void cleanUp() throw(SocketException);
115 
122  static unsigned short resolveService(const string &service,
123  const string &protocol = "tcp");
124 
125 private:
126  // Prevent the user from trying to use value semantics on this object
127  Socket(const Socket &sock);
128  void operator=(const Socket &sock);
129 
130 protected:
131  int sockDesc; // Socket descriptor
132  Socket(int type, int protocol) throw(SocketException);
133  Socket(int sockDesc);
134 };
135 
139 class CommunicatingSocket : public Socket {
140 public:
148  void connect(const string &foreignAddress, unsigned short foreignPort)
149  throw(SocketException);
150 
158  void send(const void *buffer, int bufferLen) throw(SocketException);
159 
168  int recv(void *buffer, int bufferLen) throw(SocketException);
169 
175  string getForeignAddress() throw(SocketException);
176 
182  unsigned short getForeignPort() throw(SocketException);
183 
184 protected:
185  CommunicatingSocket(int type, int protocol) throw(SocketException);
186  CommunicatingSocket(int newConnSD);
187 };
188 
193 public:
198  TCPSocket() throw(SocketException);
199 
207  TCPSocket(const string &foreignAddress, unsigned short foreignPort)
208  throw(SocketException);
209 
210 private:
211  // Access for TCPServerSocket::accept() connection creation
212  friend class TCPServerSocket;
213  TCPSocket(int newConnSD);
214 };
215 
219 class TCPServerSocket : public Socket {
220 public:
230  TCPServerSocket(unsigned short localPort, int queueLen = 5)
231  throw(SocketException);
232 
242  TCPServerSocket(const string &localAddress, unsigned short localPort,
243  int queueLen = 5) throw(SocketException);
244 
250  TCPSocket *accept() throw(SocketException);
251 
252 private:
253  void setListen(int queueLen) throw(SocketException);
254 };
255 
260 public:
265  UDPSocket() throw(SocketException);
266 
272  UDPSocket(unsigned short localPort) throw(SocketException);
273 
280  UDPSocket(const string &localAddress, unsigned short localPort)
281  throw(SocketException);
282 
288  void disconnect() throw(SocketException);
289 
300  void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
301  unsigned short foreignPort) throw(SocketException);
302 
313  int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
314  unsigned short &sourcePort) throw(SocketException);
315 
321  void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
322 
328  void joinGroup(const string &multicastGroup) throw(SocketException);
329 
335  void leaveGroup(const string &multicastGroup) throw(SocketException);
336 
337 private:
338  void setBroadcast();
339 };
340 
341 #endif