My Project
 All Classes Functions Variables Pages
Public Member Functions | Static Public Member Functions | List of all members
Socket Class Reference

Encapsulate a socket. More...

#include <Socket.h>

Public Member Functions

Socket accept ()
 Accept a new socket.
 
int bind (uint16_t port, uint32_t address)
 Bind an address/port to a socket. Specify a port of 0 to have a local port allocated. Specify an address of INADDR_ANY to use the local server IP. More...
 
int close ()
 Close the socket. More...
 
int connect (struct in_addr address, uint16_t port)
 Connect to a partner. More...
 
int connect (char *address, uint16_t port)
 Connect to a partner. More...
 
int createSocket (bool isDatagram=false)
 Create the socket. More...
 
void setReuseAddress (bool value)
 Flag the socket address as re-usable. More...
 
int setSocketOption (int option, void *value, size_t len)
 Set the socket option.
 
int setTimeout (uint32_t seconds)
 Socket timeout. More...
 
void getBind (struct sockaddr *pAddr)
 Get the bound address. More...
 
int getFD () const
 Get the underlying socket file descriptor. More...
 
bool getSSL () const
 
bool isValid ()
 
int listen (uint16_t port, bool isDatagram=false, bool reuseAddress=false)
 Create a listening socket. More...
 
bool operator< (const Socket &other) const
 
std::string readToDelim (std::string delim)
 
size_t receive (uint8_t *data, size_t length, bool exact=false)
 Receive data from the partner. Receive data from the socket partner. If exact = false, we read as much data as is available without blocking up to length. If exact = true, we will block until we have received exactly length bytes or there are no more bytes to read. More...
 
int receiveFrom (uint8_t *data, size_t length, struct sockaddr *pAddr)
 Receive data with the address. More...
 
int send (std::string value) const
 Send a string to the partner. More...
 
int send (const uint8_t *data, size_t length) const
 Send data to the partner. More...
 
int send (uint16_t value)
 
int send (uint32_t value)
 
void sendTo (const uint8_t *data, size_t length, struct sockaddr *pAddr)
 Send data to a specific address. More...
 
void setSSL (bool sslValue=true)
 Flag the socket as using SSL. More...
 
std::string toString ()
 Get the string representation of this socket. More...
 

Static Public Member Functions

static std::string addressToString (struct sockaddr *addr)
 Convert a socket address to a string representation. More...
 

Detailed Description

Encapsulate a socket.

Using this class we can connect to a partner TCP server. Once connected, we can perform send and receive requests to send and receive data. We should not attempt to send or receive until after a successful connect nor should we send or receive after closing the socket.

Member Function Documentation

std::string Socket::addressToString ( struct sockaddr *  addr)
static

Convert a socket address to a string representation.

Parameters
[in]addrThe address to parse.
Returns
A string representation of the address.
int Socket::bind ( uint16_t  port,
uint32_t  address 
)

Bind an address/port to a socket. Specify a port of 0 to have a local port allocated. Specify an address of INADDR_ANY to use the local server IP.

Parameters
[in]portPort number to bind.
[in]addressAddress to bind.
Returns
Returns 0 on success.
int Socket::close ( )

Close the socket.

Returns
Returns 0 on success.
int Socket::connect ( struct in_addr  address,
uint16_t  port 
)

Connect to a partner.

Parameters
[in]addressThe IP address of the partner.
[in]portThe port number of the partner.
Returns
Success or failure of the connection.
int Socket::connect ( char *  strAddress,
uint16_t  port 
)

Connect to a partner.

Parameters
[in]strAddressThe string representation of the IP address of the partner.
[in]portThe port number of the partner.
Returns
Success or failure of the connection.
int Socket::createSocket ( bool  isDatagram = false)

Create the socket.

Parameters
[in]isDatagramSet to true to create a datagram socket. Default is false.
Returns
The socket descriptor.
void Socket::getBind ( struct sockaddr *  pAddr)

Get the bound address.

Parameters
[out]pAddrThe storage to hold the address.
Returns
N/A.
int Socket::getFD ( ) const

Get the underlying socket file descriptor.

Returns
The underlying socket file descriptor.
int Socket::listen ( uint16_t  port,
bool  isDatagram = false,
bool  reuseAddress = false 
)

Create a listening socket.

Parameters
[in]portThe port number to listen upon.
[in]isDatagramTrue if we are listening on a datagram. The default is false.
Returns
Returns 0 on success.
size_t Socket::receive ( uint8_t *  data,
size_t  length,
bool  exact = false 
)

Receive data from the partner. Receive data from the socket partner. If exact = false, we read as much data as is available without blocking up to length. If exact = true, we will block until we have received exactly length bytes or there are no more bytes to read.

Parameters
[in]dataThe buffer into which the received data will be stored.
[in]lengthThe size of the buffer.
[in]exactRead exactly this amount.
Returns
The length of the data received or -1 on an error.
int Socket::receiveFrom ( uint8_t *  data,
size_t  length,
struct sockaddr *  pAddr 
)

Receive data with the address.

Parameters
[in]dataThe location where to store the data.
[in]lengthThe size of the data buffer into which we can receive data.
[in]pAddrAn area into which we can store the address of the partner.
Returns
The length of the data received.
int Socket::send ( std::string  value) const

Send a string to the partner.

Parameters
[in]valueThe string to send to the partner.
Returns
N/A.
int Socket::send ( const uint8_t *  data,
size_t  length 
) const

Send data to the partner.

Parameters
[in]dataThe buffer containing the data to send.
[in]lengthThe length of data to be sent.
Returns
N/A.
void Socket::sendTo ( const uint8_t *  data,
size_t  length,
struct sockaddr *  pAddr 
)

Send data to a specific address.

Parameters
[in]dataThe data to send.
[in]lengthThe length of the data to send/
[in]pAddrThe address to send the data.
void Socket::setReuseAddress ( bool  value)

Flag the socket address as re-usable.

Parameters
[in]valueTrue to mark the address as re-usable, false otherwise.
void Socket::setSSL ( bool  sslValue = true)

Flag the socket as using SSL.

Parameters
[in]sslValueTrue if we wish to use SSL.
int Socket::setTimeout ( uint32_t  seconds)

Socket timeout.

Parameters
[in]secondsto wait.
std::string Socket::toString ( )

Get the string representation of this socket.

Returns
the string representation of the socket.

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