Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
AsyncBsdSocketLib::TcpClient Class Reference

TCP client to connect to a TcpListener. More...

#include <tcp_client.h>

Inheritance diagram for AsyncBsdSocketLib::TcpClient:
AsyncBsdSocketLib::NetworkSocket AsyncBsdSocketLib::Communicator

Public Member Functions

 TcpClient (std::string ipAddress, uint16_t port)
 Constructor. More...
 
int Connection () const noexcept override
 Connection descriptor for sending and receiving. More...
 
bool IsConnected () const noexcept
 Inidicates whether the client is connected or not. More...
 
bool TrySetup () noexcept override
 Try to setup the communicator. More...
 
bool TryConnect () noexcept
 Try to connect to the server. More...
 
template<std::size_t N>
ssize_t Send (const std::array< uint8_t, N > &buffer) const noexcept
 Send a byte array to the connected client. More...
 
template<std::size_t N>
ssize_t Receive (std::array< uint8_t, N > &buffer) const noexcept
 Receive a byte array from the connected client. More...
 
- Public Member Functions inherited from AsyncBsdSocketLib::Communicator
int Descriptor () const noexcept
 File descriptor. More...
 
virtual bool TryClose () noexcept
 Try to close the communicator. More...
 

Additional Inherited Members

- Protected Member Functions inherited from AsyncBsdSocketLib::NetworkSocket
 NetworkSocket (std::string ipAddress, uint16_t port)
 Constructor. More...
 
- Protected Attributes inherited from AsyncBsdSocketLib::NetworkSocket
const std::string IpAddress
 Listening/Connecting IP address for server/client.
 
const uint16_t Port
 Listening/Connecting port number for server/client.
 
- Protected Attributes inherited from AsyncBsdSocketLib::Communicator
int FileDescriptor
 File descriptor.
 

Detailed Description

TCP client to connect to a TcpListener.

See also
TcpListener

Constructor & Destructor Documentation

◆ TcpClient()

AsyncBsdSocketLib::TcpClient::TcpClient ( std::string  ipAddress,
uint16_t  port 
)

Constructor.

Parameters
ipAddressServer IP address
portServer port number

Member Function Documentation

◆ Connection()

int AsyncBsdSocketLib::TcpClient::Connection ( ) const
overridevirtualnoexcept

Connection descriptor for sending and receiving.

Returns
A non-negative value if there is a valid connection; otherwise -1

Implements AsyncBsdSocketLib::Communicator.

◆ IsConnected()

bool AsyncBsdSocketLib::TcpClient::IsConnected ( ) const
noexcept

Inidicates whether the client is connected or not.

Returns
True if the client is connected; otherwise false

◆ Receive()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::TcpClient::Receive ( std::array< uint8_t, N > &  buffer) const
inlinenoexcept

Receive a byte array from the connected client.

Template Parameters
NReceive buffer size
Parameters
bufferReceive buffer byte array
Returns
Size of received bytes (-1 in case of receiving failed)
Warning
Due to edge-triggered polling, starvation can occur

◆ Send()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::TcpClient::Send ( const std::array< uint8_t, N > &  buffer) const
inlinenoexcept

Send a byte array to the connected client.

Template Parameters
NSend buffer size
Parameters
bufferSend buffer byte array
Returns
Size of sent bytes (-1 in case of sending failed)

◆ TryConnect()

bool AsyncBsdSocketLib::TcpClient::TryConnect ( )
noexcept

Try to connect to the server.

Returns
True if the client is successfully connected; otherwise false

◆ TrySetup()

bool AsyncBsdSocketLib::TcpClient::TrySetup ( )
overridevirtualnoexcept

Try to setup the communicator.

Returns
True if the communicator is successfully set up; otherwise false

Implements AsyncBsdSocketLib::Communicator.