Kanzi Connect  3.9.8
Kanzi Connect C++ API
kanzi::connect::Socket Class Reference

Cross platform C++ wrapper for posix sockets. More...

#include <socket.hpp>

Public Types

enum  AddressType { AddressType_IPV4, AddressType_IPV6, AddressType_InterfaceName }
 
enum  ProtocolMask { Proto_IPV4 = (1 << 0), Proto_IPV6 = (1 << 1) }
 

Public Member Functions

int accept (bool loopBack=false)
 Accept connection to socket. More...
 
bool bind (int port, bool loopBack=false, bool reuseaddr=true)
 Bind an address to socket. More...
 
bool bindToInterface (int port, const string &interfacename, bool reuseaddr)
 Binds a socket to specific port and interface. More...
 
int close ()
 
bool connect (const string &server, int port)
 Connect socket to server. More...
 
bool connect (sockaddr address)
 Connect socket to server. More...
 
bool create (int af, int type, int protocol)
 Create socket. More...
 
int getFD () const
 Returns file descriptor of this socket. More...
 
string getIPAddress ()
 Retrieves ip address of this socket. More...
 
int getPort ()
 Retrieves port of this socket. More...
 
string getRemoteIPAddress () const
 Retrieves remote ip address of this socket. More...
 
sockaddr * getSockAddr ()
 Retrieves socket address of this socket. More...
 
bool isConnected () const
 Checks whether the socket is connected. More...
 
int listen (int maxConnections)
 Listen for connections on this socket. More...
 
int read (vector< char > &data)
 Read data from socket. More...
 
int readOne (char &data)
 Read one byte of data from socket. More...
 
void setBlocking (bool blocking)
 Sets socket blocking mode. More...
 
int shutdown (bool read, bool write)
 
 Socket (int fd=-1)
 Constructor. More...
 
int write (const vector< char > &data)
 Write data to socket. More...
 
 ~Socket ()
 Destructor. More...
 

Static Public Member Functions

static unsigned long bytesAvailable (int fd)
 Non-blocking check of how much data is available for reading from descriptor. More...
 
static enum AddressType determineAddressType (const string &candidate)
 Determines a type of given address. More...
 
static vector< string > getBindableAddresses (const string &interfacename, size_t mask=(Proto_IPV4|Proto_IPV6))
 Returns a list of addresses that belong into specific interface. More...
 
static bool getIPAndPort (sockaddr_storage *sender, string &ip, string &port)
 Translate sockaddr into ip address and port number. More...
 
static bool looksLikeIPV6Address (const string &address)
 Checks whether given address looks like a IPv6 address. More...
 
static int read (int fd, vector< char > &data)
 Read all data from file descriptor to data buffer. More...
 
static int readOne (int fd, char &data)
 Read one byte from a file descriptor to a character. More...
 

Protected Member Functions

bool isDatagram () const
 Checks is the socket datagram type. More...
 

Detailed Description

Cross platform C++ wrapper for posix sockets.

Member Enumeration Documentation

◆ AddressType

Enumerator
AddressType_IPV4 

Specific address is recognized to be IPv4.

AddressType_IPV6 

Specific address is recognized to be IPv6.

AddressType_InterfaceName 

Address is likely a interface name.

◆ ProtocolMask

Enumerator
Proto_IPV4 
Proto_IPV6 

Constructor & Destructor Documentation

◆ Socket()

kanzi::connect::Socket::Socket ( int  fd = -1)

Constructor.

Parameters
fdFile descriptor of a socket, or -1 for closed socket.

◆ ~Socket()

kanzi::connect::Socket::~Socket ( )

Destructor.

Member Function Documentation

◆ accept()

int kanzi::connect::Socket::accept ( bool  loopBack = false)

Accept connection to socket.

Parameters
loopBackAccept only connections from this device.
Returns
File descriptor of the accepted socket or -1 for error.

◆ bind()

bool kanzi::connect::Socket::bind ( int  port,
bool  loopBack = false,
bool  reuseaddr = true 
)

Bind an address to socket.

Parameters
portPort to bind.
loopBackBind to address of this device.
reuseaddrif true then SO_REUSEADDR is opted to socket.
Returns
True if operation was successful, otherwise false.

◆ bindToInterface()

bool kanzi::connect::Socket::bindToInterface ( int  port,
const string &  interfacename,
bool  reuseaddr 
)

Binds a socket to specific port and interface.

Parameters
portport number where to bind
interfacenameeither name or ip address of the interface to bind to.
reuseaddrif true then SO_REUSEADDR is opted to socket.
Returns
true if bound successfully, false if some errors were met.

◆ bytesAvailable()

static unsigned long kanzi::connect::Socket::bytesAvailable ( int  fd)
static

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

Parameters
fdFile descriptor.
Returns
Amount of data available.

◆ close()

int kanzi::connect::Socket::close ( )
Returns
If successful, returns 0. Otherwise returns -1 as error.

◆ connect() [1/2]

bool kanzi::connect::Socket::connect ( const string &  server,
int  port 
)

Connect socket to server.

Parameters
serverServer address.
portServer port.
Returns
True if socket was successfully connected.

◆ connect() [2/2]

bool kanzi::connect::Socket::connect ( sockaddr  address)

Connect socket to server.

Parameters
addressAddress of the server.
Returns
True if socket was successfully connected.

◆ create()

bool kanzi::connect::Socket::create ( int  af,
int  type,
int  protocol 
)

Create socket.

Parameters
afAddress family.
typeSocket type.
protocolProtocol type.
Returns
True if socket was successfully created.

◆ determineAddressType()

static enum AddressType kanzi::connect::Socket::determineAddressType ( const string &  candidate)
static

Determines a type of given address.

Parameters
candidatethe candidate to check.
Returns
enumeration value. if empty string is passed then its recognized as interface.

◆ getBindableAddresses()

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

Returns a list of addresses that belong into specific interface.

Parameters
interfacenameThe name of the interface for which to seach.
maskThe address types to return.
Returns
vector of addresses bound to specific interface.

◆ getFD()

int kanzi::connect::Socket::getFD ( ) const

Returns file descriptor of this socket.

Returns
File descriptor of this socket.

◆ getIPAddress()

string kanzi::connect::Socket::getIPAddress ( )

Retrieves ip address of this socket.

Returns
Ip address of this socket as string.

◆ getIPAndPort()

static bool kanzi::connect::Socket::getIPAndPort ( sockaddr_storage *  sender,
string &  ip,
string &  port 
)
static

Translate sockaddr into ip address and port number.

Parameters
senderthe sender address
ipthe ip address
portthe port number.
Returns
true if retrieval succeeded, false on error.

◆ getPort()

int kanzi::connect::Socket::getPort ( )

Retrieves port of this socket.

Returns
port of this socket.

◆ getRemoteIPAddress()

string kanzi::connect::Socket::getRemoteIPAddress ( ) const

Retrieves remote ip address of this socket.

Returns
Ip address of the remote side of socket as string.

◆ getSockAddr()

sockaddr* kanzi::connect::Socket::getSockAddr ( )

Retrieves socket address of this socket.

Returns
Socket address of this socket.

◆ isConnected()

bool kanzi::connect::Socket::isConnected ( ) const

Checks whether the socket is connected.

Returns
true if socket still holds a functional connection.

◆ isDatagram()

bool kanzi::connect::Socket::isDatagram ( ) const
protected

Checks is the socket datagram type.

Returns
true if datagram type of socket

◆ listen()

int kanzi::connect::Socket::listen ( int  maxConnections)

Listen for connections on this socket.

Parameters
maxConnectionsMaximum amount of connections.
Returns
If successful, returns 0. Otherwise returns -1 as error.

◆ looksLikeIPV6Address()

static bool kanzi::connect::Socket::looksLikeIPV6Address ( const string &  address)
static

Checks whether given address looks like a IPv6 address.

Parameters
addressthe address to examine
Returns
true if looks like ipv6 address.

◆ read() [1/2]

static int kanzi::connect::Socket::read ( int  fd,
vector< char > &  data 
)
static

Read all data from file descriptor to data buffer.

Parameters
fdFile descriptor.
dataOutput data.
Returns
Amount of data read, zero for EOF or -1 for error.

◆ read() [2/2]

int kanzi::connect::Socket::read ( vector< char > &  data)

Read data from socket.

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

◆ readOne() [1/2]

static int kanzi::connect::Socket::readOne ( int  fd,
char &  data 
)
static

Read one byte from a file descriptor to a character.

Parameters
fdFile descriptor.
databyte where to read.
Returns
Amount of data read, zero for EOF or -1 for error.

◆ readOne() [2/2]

int kanzi::connect::Socket::readOne ( char &  data)

Read one byte of data from socket.

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

◆ setBlocking()

void kanzi::connect::Socket::setBlocking ( bool  blocking)

Sets socket blocking mode.

Parameters
blockingIf true, sets socket to blocking mode.

◆ shutdown()

int kanzi::connect::Socket::shutdown ( bool  read,
bool  write 
)
Parameters
readif true, receptions will be disabled.
writeif true, transmissions will be disabled.
Returns
If successful, returns 0. Otherwise returns -1 as error.

◆ write()

int kanzi::connect::Socket::write ( const vector< char > &  data)

Write data to socket.

Parameters
dataData to write.
Returns
Amount of data written, zero for EOF or -1 for error.

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