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.

Return

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

Parameters
  • loopBack: Accept only connections from this device.

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

Bind an address to socket.

Return

True if operation was successful, otherwise false.

Parameters
  • port: Port to bind.

  • loopBack: Bind to address of this device.

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

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

Binds a socket to specific port and interface.

Return

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

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.

int close()

Return

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

bool connect(const string &server, int port)

Connect socket to server.

Return

True if socket was successfully connected.

Parameters
  • server: Server address.

  • port: Server port.

bool connect(sockaddr address)

Connect socket to server.

Return

True if socket was successfully connected.

Parameters
  • address: Address of the server.

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

Create socket.

Return

True if socket was successfully created.

Parameters
  • af: Address family.

  • type: Socket type.

  • protocol: Protocol type.

int getFD() const

Returns file descriptor of this socket.

Return

File descriptor of this socket.

string getIPAddress()

Retrieves ip address of this socket.

Return

Ip address of this socket as string.

int getPort()

Retrieves port of this socket.

Return

port of this socket.

string getRemoteIPAddress() const

Retrieves remote ip address of this socket.

Return

Ip address of the remote side of socket as string.

sockaddr *getSockAddr()

Retrieves socket address of this socket.

Return

Socket address of this socket.

bool isConnected() const

Checks whether the socket is connected.

Return

true if socket still holds a functional connection.

int listen(int maxConnections)

Listen for connections on this socket.

Return

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

Parameters
  • maxConnections: Maximum amount of connections.

int read(vector<char> &data)

Read data from socket.

Return

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

Parameters
  • data: Output data.

int readOne(char &data)

Read one byte of data from socket.

Return

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

Parameters
  • data: Output data.

void setBlocking(bool blocking)

Sets socket blocking mode.

Parameters
  • blocking: If true, sets socket to blocking mode.

int shutdown(bool read, bool write)

Return

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

Parameters
  • read: if true, receptions will be disabled.

  • write: if true, transmissions will be disabled.

int write(const vector<char> &data)

Write data to socket.

Return

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

Parameters
  • data: Data to write.

Public Static Functions

static unsigned long bytesAvailable(int fd)

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

Return

Amount of data available.

Parameters
  • fd: File descriptor.

static enum AddressType determineAddressType(const string &candidate)

Determines a type of given address.

Return

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

Parameters
  • candidate: the candidate to check.

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

Returns a list of addresses that belong into specific interface.

Return

vector of addresses bound to specific interface.

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

  • mask: The address types to return.

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

Translate sockaddr into ip address and port number.

Return

true if retrieval succeeded, false on error.

Parameters
  • sender: the sender address

  • ip: the ip address

  • port: the port number.

static bool looksLikeIPV6Address(const string &address)

Checks whether given address looks like a IPv6 address.

Return

true if looks like ipv6 address.

Parameters
  • address: the address to examine

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

Read all data from file descriptor to data buffer.

Return

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

Parameters
  • fd: File descriptor.

  • data: Output data.

static int readOne(int fd, char &data)

Read one byte from a file descriptor to a character.

Return

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

Parameters
  • fd: File descriptor.

  • data: byte where to read.