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... | |
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.
|
static |
Convert a socket address to a string representation.
[in] | addr | The address to parse. |
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.
[in] | port | Port number to bind. |
[in] | address | Address to bind. |
int Socket::close | ( | ) |
Close the socket.
int Socket::connect | ( | struct in_addr | address, |
uint16_t | port | ||
) |
Connect to a partner.
[in] | address | The IP address of the partner. |
[in] | port | The port number of the partner. |
int Socket::connect | ( | char * | strAddress, |
uint16_t | port | ||
) |
Connect to a partner.
[in] | strAddress | The string representation of the IP address of the partner. |
[in] | port | The port number of the partner. |
int Socket::createSocket | ( | bool | isDatagram = false | ) |
Create the socket.
[in] | isDatagram | Set to true to create a datagram socket. Default is false. |
void Socket::getBind | ( | struct sockaddr * | pAddr | ) |
Get the bound address.
[out] | pAddr | The storage to hold the address. |
int Socket::getFD | ( | ) | const |
Get the underlying socket file descriptor.
int Socket::listen | ( | uint16_t | port, |
bool | isDatagram = false , |
||
bool | reuseAddress = false |
||
) |
Create a listening socket.
[in] | port | The port number to listen upon. |
[in] | isDatagram | True if we are listening on a datagram. The default is false. |
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.
[in] | data | The buffer into which the received data will be stored. |
[in] | length | The size of the buffer. |
[in] | exact | Read exactly this amount. |
int Socket::receiveFrom | ( | uint8_t * | data, |
size_t | length, | ||
struct sockaddr * | pAddr | ||
) |
Receive data with the address.
[in] | data | The location where to store the data. |
[in] | length | The size of the data buffer into which we can receive data. |
[in] | pAddr | An area into which we can store the address of the partner. |
int Socket::send | ( | std::string | value | ) | const |
Send a string to the partner.
[in] | value | The string to send to the partner. |
int Socket::send | ( | const uint8_t * | data, |
size_t | length | ||
) | const |
Send data to the partner.
[in] | data | The buffer containing the data to send. |
[in] | length | The length of data to be sent. |
void Socket::sendTo | ( | const uint8_t * | data, |
size_t | length, | ||
struct sockaddr * | pAddr | ||
) |
Send data to a specific address.
[in] | data | The data to send. |
[in] | length | The length of the data to send/ |
[in] | pAddr | The address to send the data. |
void Socket::setReuseAddress | ( | bool | value | ) |
Flag the socket address as re-usable.
[in] | value | True to mark the address as re-usable, false otherwise. |
void Socket::setSSL | ( | bool | sslValue = true | ) |
Flag the socket as using SSL.
[in] | sslValue | True if we wish to use SSL. |
int Socket::setTimeout | ( | uint32_t | seconds | ) |
Socket timeout.
[in] | seconds | to wait. |
std::string Socket::toString | ( | ) |
Get the string representation of this socket.