LRAUV  revA
Socket.h
Go to the documentation of this file.
1 
9 #ifndef SOCKET_H_
10 #define SOCKET_H_
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netdb.h>
16 #include <unistd.h>
17 #include <arpa/inet.h>
18 
19 #include "SocketException.h"
20 
21 const int MAXHOSTNAME = 200;
22 const int MAXCONNECTIONS = 5;
23 const int MAXRECV = 500;
24 const int SIMPORT = 54327;
25 
33 class Socket
34 {
35 public:
36 
37  virtual ~Socket();
38  // Data Transimission
39  int send( const char* buffer, size_t bufferLength ) const throw( SocketException& );
40 
46  int sendall( const char* buffer, size_t bufferLength );
47 
49  int recv( char* buffer, size_t bufferLength ) const throw( SocketException& );
50 
54  int recv( char* buffer, size_t bufferLength, int timeoutMillis ) throw( SocketException& );
55 
56  int setTimeout( const int milliseconds );
57  int close();
58  int getPort()
59  {
60  return addr_.sin_port;
61  }
62 
65  {
66  return sock_;
67  }
68 
69 protected:
70 
71  Socket();
72 
73  // Server initialization
74  void create() throw( SocketException& );
75  void bind( const int port ) throw( SocketException& );
76  void listen() const throw( SocketException& );
77  void accept( Socket& newSocket ) throw( SocketException& );
78 
80  void accept( Socket& newSocket, int timeoutMillis ) throw( SocketException& );
81 
82  // Client initialization
83  void connect( const char* host, const int port ) throw( SocketException& );
84 
88  void connect( const char* host, const int port, int timeoutMillis ) throw( SocketException& );
89 
90  void setNonBlocking( const bool nonBlocking ) throw( SocketException& );
91 
92  bool isValid() const
93  {
94  return sock_ != -1;
95  }
96 
97  void setTimeval( const int milliseconds, struct timeval& tv );
98 
99 private:
100 
101  int sock_;
102  sockaddr_in addr_;
104 };
105 
106 #endif /*SOCKET_H_*/
A wrapper around sys/socket.h for tcp communications between Socket instances – use daughter classes...
Definition: Socket.h:33
Definition: SocketException.h:6
sockaddr_in addr_
Definition: Socket.h:102
const int MAXCONNECTIONS
Definition: Socket.h:22
void create()
Definition: Socket.cpp:32
int close()
Definition: Socket.cpp:273
bool isValid() const
Definition: Socket.h:92
bool connected_
Definition: Socket.h:103
void bind(const int port)
Definition: Socket.cpp:51
const int MAXHOSTNAME
Definition: Socket.h:21
int sendall(const char *buffer, size_t bufferLength)
Like send but making sure the whole requested buffer is sent out.
Definition: Socket.cpp:166
int sock_
Definition: Socket.h:101
virtual ~Socket()
Definition: Socket.cpp:24
int getPort()
Definition: Socket.h:58
void listen() const
Definition: Socket.cpp:67
void accept(Socket &newSocket)
Definition: Socket.cpp:79
void setNonBlocking(const bool nonBlocking)
Definition: Socket.cpp:458
const int MAXRECV
Definition: Socket.h:23
Socket()
Definition: Socket.cpp:17
const int SIMPORT
Definition: Socket.h:24
int setTimeout(const int milliseconds)
Definition: Socket.cpp:259
int send(const char *buffer, size_t bufferLength) const
If return value is -1, check errno.
Definition: Socket.cpp:148
void connect(const char *host, const int port)
Definition: Socket.cpp:286
void setTimeval(const int milliseconds, struct timeval &tv)
Definition: Socket.cpp:267
int recv(char *buffer, size_t bufferLength) const
Blocking read.
Definition: Socket.cpp:196
int getDescriptor()
exposes the socket descriptor to facilitate direct operations
Definition: Socket.h:64