Socket

class kanzi::connect::Socket

Cross platform C++ wrapper for posix sockets.

Public Types

enum AddressType

Values:

enumerator AddressType_IPV4

Specific address is recognized to be IPv4.

enumerator AddressType_IPV6

Specific address is recognized to be IPv6.

enumerator AddressType_InterfaceName

Address is likely a interface name.

enum ProtocolMask

Values:

enumerator Proto_IPV4
enumerator Proto_IPV6

Public Functions

Socket(int fd = -1)

Constructor.

Parameters

fd – File descriptor of a socket, or -1 for closed socket.

~Socket()

Destructor.

int accept(bool loopBack = false)

Accept connection to socket.

Parameters

loopBack – Accept only connections from this device.

Returns

File descriptor of the accepted socket or -1 for error.

bool bind(int port, bool loopBack = false, bool reuseaddr = true)

Bind an address to socket.

Parameters
  • port – Port to bind.

  • loopBack – Bind to address of this device.

  • reuseaddr – if true then SO_REUSEADDR is opted to socket.

Returns

True if operation was successful, otherwise false.

bool bindToInterface(int port, const string &interfacename, bool reuseaddr)

Binds a socket to specific port and interface.

Parameters
  • port – port number where to bind

  • interfacename – either name or ip address of the interface to bind to.

  • reuseaddr – if true then SO_REUSEADDR is opted to socket.

Returns

true if bound successfully, false if some errors were met.

int close()
Returns

If successful, returns 0. Otherwise returns -1 as error.

bool connect(const string &server, int port)

Connect socket to server.

Parameters
  • server – Server address.

  • port – Server port.

Returns

True if socket was successfully connected.

bool connect(sockaddr address)

Connect socket to server.

Parameters

address – Address of the server.

Returns

True if socket was successfully connected.

bool create(int af, int type, int protocol)

Create socket.

Parameters
  • af – Address family.

  • type – Socket type.

  • protocol – Protocol type.

Returns

True if socket was successfully created.

int getFD() const

Returns file descriptor of this socket.

Returns

File descriptor of this socket.

string getIPAddress()

Retrieves ip address of this socket.

Returns

Ip address of this socket as string.

int getPort()

Retrieves port of this socket.

Returns

port of this socket.

string getRemoteIPAddress() const

Retrieves remote ip address of this socket.

Returns

Ip address of the remote side of socket as string.

sockaddr *getSockAddr()

Retrieves socket address of this socket.

Returns

Socket address of this socket.

bool isConnected() const

Checks whether the socket is connected.

Returns

true if socket still holds a functional connection.

int listen(int maxConnections)

Listen for connections on this socket.

Parameters

maxConnections – Maximum amount of connections.

Returns

If successful, returns 0. Otherwise returns -1 as error.

int read(vector<char> &data)

Read data from socket.

Parameters

data – Output data.

Returns

Amount of data read, zero for EOF or -1 for error.

int readOne(char &data)

Read one byte of data from socket.

Parameters

data – Output data.

Returns

Amount of data read, zero for EOF or -1 for error.

void setBlocking(bool blocking)

Sets socket blocking mode.

Parameters

blocking – If true, sets socket to blocking mode.

int shutdown(bool read, bool write)
Parameters
  • read – if true, receptions will be disabled.

  • write – if true, transmissions will be disabled.

Returns

If successful, returns 0. Otherwise returns -1 as error.

int write(const vector<char> &data)

Write data to socket.

Parameters

data – Data to write.

Returns

Amount of data written, zero for EOF or -1 for error.

Public Static Functions

static unsigned long bytesAvailable(int fd)

Non-blocking check of how much data is available for reading from descriptor.

Parameters

fd – File descriptor.

Returns

Amount of data available.

static enum AddressType determineAddressType(const string &candidate)

Determines a type of given address.

Parameters

candidate – the candidate to check.

Returns

enumeration value. if empty string is passed then its recognized as interface.

static vector<string> getBindableAddresses(const string &interfacename, size_t mask = (Proto_IPV4 | Proto_IPV6))

Returns a list of addresses that belong into specific interface.

Parameters
  • interfacename – The name of the interface for which to seach.

  • mask – The address types to return.

Returns

vector of addresses bound to specific interface.

static bool getIPAndPort(sockaddr_storage *sender, string &ip, string &port)

Translate sockaddr into ip address and port number.

Parameters
  • sender – the sender address

  • ip – the ip address

  • port – the port number.

Returns

true if retrieval succeeded, false on error.

static bool looksLikeIPV6Address(const string &address)

Checks whether given address looks like a IPv6 address.

Parameters

address – the address to examine

Returns

true if looks like ipv6 address.

static int read(int fd, vector<char> &data)

Read all data from file descriptor to data buffer.

Parameters
  • fd – File descriptor.

  • data – Output data.

Returns

Amount of data read, zero for EOF or -1 for error.

static int readOne(int fd, char &data)

Read one byte from a file descriptor to a character.

Parameters
  • fd – File descriptor.

  • data – byte where to read.

Returns

Amount of data read, zero for EOF or -1 for error.