EasySockets v2.0.0
A simple, cross platform socket library for C++.
Loading...
Searching...
No Matches

An object which represents a connection between two applications. More...

#include <PosixSocket.hpp>

Public Member Functions

 PosixSocket (IpVersion ip_version, Protocol protocol)
 Constructor for Socket.
 ~PosixSocket ()
 PosixSocket (const PosixSocket &)=delete
PosixSocketoperator= (const PosixSocket &)=delete
 PosixSocket (PosixSocket &&other) noexcept
PosixSocketoperator= (PosixSocket &&other) noexcept
void bind_to (const EndPoint &end_point)
 Binds a socket to the given end point.
void connect_to (const EndPoint &end_point)
 Connects a socket to the given end point.
void listen_for_connections (int backlog)
 Makes the socket start listening for incoming connections.
PosixSocket accept_connection ()
 Accepts incoming connections.
int64_t receive_data (char *buffer, int buffer_size)
 Waits to receive data from the connected end point.
int64_t receive_data_from (char *buffer, int buffer_size, EndPoint &sender_end_point)
 Waits to receive data from any endpoint.
int64_t send_data (const char *buffer, int buffer_size)
 Sends data to the connected end point.
int64_t send_data_to (const char *buffer, int buffer_size, const EndPoint &end_point)
 Sends data to the provided end point.

Detailed Description

An object which represents a connection between two applications.

On creation, type of ip addresses that the socket will use and the layer 4/transport layer protocol is specified.
From this, methods like .bind_to(EndPoint end_point) and .connect_to(EndPoint end_point) to give the socket an address and port so that it can start communicating with other applications.
For connectionless protocols like UDP, data can be sent and received without binding or connecting first.
Can be moved from but not copied. Members are the same across all implementations.

Constructor & Destructor Documentation

◆ PosixSocket() [1/3]

es::PosixSocket::PosixSocket ( IpVersion ip_version,
Protocol protocol )

Constructor for Socket.

Parameters
ip_versionThe version of ip addresses that the socket will use.
protocolThe layer 4/transport layer protocol that the socket will use.

◆ ~PosixSocket()

es::PosixSocket::~PosixSocket ( )

◆ PosixSocket() [2/3]

es::PosixSocket::PosixSocket ( const PosixSocket & )
delete

◆ PosixSocket() [3/3]

es::PosixSocket::PosixSocket ( PosixSocket && other)
noexcept

Member Function Documentation

◆ accept_connection()

PosixSocket es::PosixSocket::accept_connection ( )

Accepts incoming connections.

.listen(int backlog)must be called first.

Returns
New socket connected to the socket that was attempting to connect.

◆ bind_to()

void es::PosixSocket::bind_to ( const EndPoint & end_point)

Binds a socket to the given end point.

Parameters
end_pointThe endpoint that the socket will use. "0.0.0.0" will bind to all IPv4 interfaces, "::0"will bind to all IPv6 interfaces and "::"" will bind to all interfaces.

◆ connect_to()

void es::PosixSocket::connect_to ( const EndPoint & end_point)

Connects a socket to the given end point.

Parameters
end_pointThe endpoint that the socket will use.

◆ listen_for_connections()

void es::PosixSocket::listen_for_connections ( int backlog)

Makes the socket start listening for incoming connections.

Must be bound with .bind(EndPoint end_point) before being called.

Parameters
backlogThe amount of connections that can be waiting to be accepted at a time. If the amount of connections exceeds the backlog, then new connections will be dropped.
Connections can be accepted with .accept_connection.

◆ operator=() [1/2]

PosixSocket & es::PosixSocket::operator= ( const PosixSocket & )
delete

◆ operator=() [2/2]

PosixSocket & es::PosixSocket::operator= ( PosixSocket && other)
noexcept

◆ receive_data()

int64_t es::PosixSocket::receive_data ( char * buffer,
int buffer_size )

Waits to receive data from the connected end point.

Must be connected to another end point.

Parameters
bufferThe buffer which data received will be placed into.
buffer_sizeThe size of the buffer provided.
Returns
The amount of data in bytes received.

◆ receive_data_from()

int64_t es::PosixSocket::receive_data_from ( char * buffer,
int buffer_size,
EndPoint & sender_end_point )

Waits to receive data from any endpoint.

Can only be used with connectionless protocols like UDP.

Parameters
bufferThe buffer which data received will be placed into.
buffer_sizeThe size of the buffer provided.
sender_end_pointAn out parameter will give back the end point of the sender.
Returns
The amount of data in bytes received.

◆ send_data()

int64_t es::PosixSocket::send_data ( const char * buffer,
int buffer_size )

Sends data to the connected end point.

Must be connected to another end point.

Parameters
bufferThe buffer which data received will be placed into.
buffer_sizeThe size of the buffer provided.
Returns
The amount of data in bytes sent.

◆ send_data_to()

int64_t es::PosixSocket::send_data_to ( const char * buffer,
int buffer_size,
const EndPoint & end_point )

Sends data to the provided end point.

Must be connected to another end point.

Parameters
bufferThe buffer which data received will be placed into.
buffer_sizeThe size of the buffer provided.
end_pointThe endpoint that the data will be sent to.
Returns
The amount of data in bytes sent.