TCP classes

The TCP classes can be used with imebra::StreamWriter and imebra::StreamReader to send and receive data through a TCP stream.

A TCP server socket (a socket that listen for incoming connection) can be realized with the class imebra::TCPListener, while a TCP client (a socket that initiates a connection with a server) can be realized with the class imebra::TCPStream.

imebra::TCPListener creates a new imebra::TCPStream for each accepted incoming connection.

Both imebra::TCPListener and imebra::TCPStream expose blocking methods (except for the constructors which connect to the peer in non-blocking mode). You can exit a blocking method to terminate by closing the socket or by calling the terminate() method.

TCPAddress

class TCPAddress

Represents a TCP address.

Use TCPActiveAddress to manage an address of an active socket (a socket that initiates the connection) and TCPPassiveAddress for a passive socket (a socket that listens for incoming connections).

Subclassed by imebra::TCPActiveAddress, imebra::TCPPassiveAddress

Public Functions

std::string getNode() const

Returns the node part of the TCP address.

The TCP address is composed by two parts:

  • the node which identifies the machine in the network
  • the service which identifies one of the ports (services) in the machine

Return
the node part of the TCP address

std::string getService() const

Returns the service part of the TCP address.

See getNode() for more information.

Return
the service part of the TCP address

TCPActiveAddress

class TCPActiveAddress : public imebra::TCPAddress

Represents an address of an active socket (a socket that initiates the connection with the peer).

Public Functions

TCPActiveAddress(const std::string &node, const std::string &service)

Constructor.

Constructs an active TCP address from a node and a service description.

The node may be a host name or address (both IPv4 and IPv6) while the service name may be a port number (in string format) or name (e.g. “FTP”).

Parameters
  • node: the host name or address (e.g. “192.168.10.20” or “example.com”). Use an empty string to refer to the local host.
  • service: the service port (in string format) or name (e.g. “140” or “ftp”).

TCPPassiveAddress

class TCPPassiveAddress : public imebra::TCPAddress

Represents an address of a passive socket (a socket that listens for connections initiated by the peers).

Public Functions

TCPPassiveAddress(const std::string &node, const std::string &service)

Constructor.

Constructs an active TCP address from a node and a service description.

The node may be a host name or address (both IPv4 and IPv6) while the service name may be a port number (in string format) or name (e.g. “FTP”).

Parameters
  • node: the host name or address (e.g. “192.168.10.20” or “example.com”). Use an empty string to refer to all the TCP devices on the local host.
  • service: the service port (in string format) or name (e.g. “140” or “ftp”).

TCPStream

class TCPStream : public imebra::BaseStreamInput, public imebra::BaseStreamOutput

Represents a TCP stream.

Public Functions

TCPStream(const TCPActiveAddress &address)

Construct a TCP socket and connects it to the destination address.

This is a non-blocking operation (the connection proceed after the constructor returns). Connection errors will be reported later while the communication happens.

Parameters
  • address: the address to which the socket has to be connected.

TCPAddress *getPeerAddress() const

Returns the address of the connected peer.

Return
the address of the connected peer

void terminate()

Instruct any pending operation to terminate.

Current and subsequent read and write operations will fail by throwing the exception StreamClosedError.

TCPListener

class TCPListener

Represents listening TCP socket.

Once allocated the socket starts listening at the address declared in the constructor.

A loop in the client application should call waitForConnection() in order to retrieve all the connections accepted by the socket.

Public Functions

TCPListener(const TCPPassiveAddress &address)

Constructor.

Constructs a listening socket and starts listening for incoming connections.

Parameters
  • address: the address to which the listening socket must be bound

TCPStream *waitForConnection()

Waits for an incoming connection on the listening socket.

The method blocks until a new connection is accepted or until the socket is closed, in which case it throws the exception StreamClosedError.

The socket is closed by the TCPStream’s destructor or by a call to terminate().

Return
a new TCPStream object bound to the new accepted connection. the returned TCPStream object will be owned by the caller

void terminate()

Instruct the listener to terminate any pending action.

If a thread is in the method waitForConnection() then it will receive the exception StreamClosedError. StreamClosedError will be also thrown for each subsequent call to waitForConnection().