LRAUV  revA
Socket Class Reference

A wrapper around sys/socket.h for tcp communications between Socket instances – use daughter classes SocketClient or SocketServer to do any real work. More...

#include <Socket.h>

Inheritance diagram for Socket:

Public Member Functions

virtual ~Socket ()
 
int send (const char *buffer, size_t bufferLength) const throw ( SocketException& )
 If return value is -1, check errno. More...
 
int sendall (const char *buffer, size_t bufferLength)
 Like send but making sure the whole requested buffer is sent out. More...
 
int recv (char *buffer, size_t bufferLength) const throw ( SocketException& )
 Blocking read. More...
 
int recv (char *buffer, size_t bufferLength, int timeoutMillis) throw ( SocketException& )
 Socket read with timeout. More...
 
int setTimeout (const int milliseconds)
 
int close ()
 
int getPort ()
 
int getDescriptor ()
 exposes the socket descriptor to facilitate direct operations More...
 

Protected Member Functions

 Socket ()
 
void create () throw ( SocketException& )
 
void bind (const int port) throw ( SocketException& )
 
void listen () const throw ( SocketException& )
 
void accept (Socket &newSocket) throw ( SocketException& )
 
void accept (Socket &newSocket, int timeoutMillis) throw ( SocketException& )
 accept with given timeout, which could be 0 for completely non-blocking behavior More...
 
void connect (const char *host, const int port) throw ( SocketException& )
 
void connect (const char *host, const int port, int timeoutMillis) throw ( SocketException& )
 connect with timeout. More...
 
void setNonBlocking (const bool nonBlocking) throw ( SocketException& )
 
bool isValid () const
 
void setTimeval (const int milliseconds, struct timeval &tv)
 

Private Attributes

int sock_
 
sockaddr_in addr_
 
bool connected_
 

Detailed Description

A wrapper around sys/socket.h for tcp communications between Socket instances – use daughter classes SocketClient or SocketServer to do any real work.

Constructor & Destructor Documentation

Socket::~Socket ( )
virtual

References close(), isValid(), and sock_.

Socket::Socket ( )
protected

References addr_.

Member Function Documentation

void Socket::accept ( Socket newSocket)
throw (SocketException &
)
protected

Referenced by SocketServer::accept().

void Socket::accept ( Socket newSocket,
int  timeoutMillis 
)
throw (SocketException &
)
protected

accept with given timeout, which could be 0 for completely non-blocking behavior

void Socket::bind ( const int  port)
throw (SocketException &
)
protected
void Socket::connect ( const char *  host,
const int  port 
)
throw (SocketException &
)
protected
void Socket::connect ( const char *  host,
const int  port,
int  timeoutMillis 
)
throw (SocketException &
)
protected

connect with timeout.

See SocketClient( const char* host, int port, int timeoutMillis ) constructor in SocketClient.h for general documentation about this connection mode.

void Socket::create ( )
throw (SocketException &
)
protected
int Socket::getDescriptor ( )
inline

exposes the socket descriptor to facilitate direct operations

References sock_.

Referenced by ESPComm::acceptClient(), ESPComm::connectAsClient(), and ESPComm::getPeerAddress().

int Socket::getPort ( )
inline

References addr_.

bool Socket::isValid ( ) const
inlineprotected

References sock_.

Referenced by create(), listen(), and ~Socket().

void Socket::listen ( ) const
throw (SocketException &
)
protected

References isValid(), MAXCONNECTIONS, and sock_.

Referenced by SocketServer::SocketServer().

int Socket::recv ( char *  buffer,
size_t  bufferLength 
) const
throw (SocketException &
)

Blocking read.

If return value is -1, check errno.

Referenced by ExternalSim::initialize(), ExternalSim::run(), and SimDaemon::ServiceClient().

int Socket::recv ( char *  buffer,
size_t  bufferLength,
int  timeoutMillis 
)
throw (SocketException &
)

Socket read with timeout.

If return value is -1, check errno.

NOTE: assumes the socket is in blocking mode: it sets it to non-blocking for the internal operation, and sets it back to blocking before returning.

int Socket::send ( const char *  buffer,
size_t  bufferLength 
) const
throw (SocketException &
)

If return value is -1, check errno.

Referenced by ExternalSim::initialize(), ExternalSim::run(), sendall(), and SimDaemon::ServiceClient().

int Socket::sendall ( const char *  buffer,
size_t  bufferLength 
)

Like send but making sure the whole requested buffer is sent out.

Returns number of bytes sent, or: -2: unconnected socket -1: some system error; check errno Note: for the moment using return code for error condition instead of throwing exception.

References connected_, send(), and sock_.

Referenced by ESPComm::acceptClient(), ESPComm::connectAsClient(), ESPComm::sendLineAux(), and ESPComm::submitAbort().

void Socket::setNonBlocking ( const bool  nonBlocking)
throw (SocketException &
)
protected
int Socket::setTimeout ( const int  milliseconds)

References sock_.

Referenced by SimDaemon::ServiceClient().

void Socket::setTimeval ( const int  milliseconds,
struct timeval &  tv 
)
protected

Member Data Documentation

sockaddr_in Socket::addr_
private

Referenced by getPort(), and Socket().

bool Socket::connected_
private

Referenced by close(), and sendall().

int Socket::sock_
private

The documentation for this class was generated from the following files: