EasySockets v2.2.1
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 ()
 Default constructs a socket.
 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.
void close ()
 Closes the socket.

Static Public Member Functions

static PosixSocket make_connected_tcp (const EndPoint &end_point)
 Makes a TCP socket connected to the given endpoint.
static PosixSocket make_connected_udp (const EndPoint &end_point)
 Makes a UDP socket connected to the given endpoint.
static PosixSocket make_bound_tcp (Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
 Makes a TCP socket bound to the given port.
static PosixSocket make_bound_udp (Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
 Makes a UDP socket bound to the given port.

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/4]

es::PosixSocket::PosixSocket ( )

Default constructs a socket.

This socket will not be usable and will have to be assigned to something else.

◆ PosixSocket() [2/4]

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() [3/4]

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

◆ PosixSocket() [4/4]

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.

◆ close()

void es::PosixSocket::close ( )

Closes the socket.

Is automatically called on destruction. Will make blocking methods on socket that are currently beign executed throw.

◆ 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.

◆ make_bound_tcp()

PosixSocket es::PosixSocket::make_bound_tcp ( Port port,
IpVersion ip_version = IpVersion::DUAL_STACK )
static

Makes a TCP socket bound to the given port.

Parameters
portThe port which the socket will be bound to.
ip_versionOptional paramater for the IP version that the socket will use. Defaults to IpVersion::DUAL_STACK.
Returns
The bound socket.

◆ make_bound_udp()

PosixSocket es::PosixSocket::make_bound_udp ( Port port,
IpVersion ip_version = IpVersion::DUAL_STACK )
static

Makes a UDP socket bound to the given port.

Parameters
portThe port which the socket will be bound to.
ip_versionOptional paramater for the IP version that the socket will use. Defaults to IpVersion::DUAL_STACK.
Returns
The bound socket.

◆ make_connected_tcp()

PosixSocket es::PosixSocket::make_connected_tcp ( const EndPoint & end_point)
static

Makes a TCP socket connected to the given endpoint.

Parameters
end_pointThe endpoint that the socket is attempting to connect to.
Returns
The connected socket.

◆ make_connected_udp()

PosixSocket es::PosixSocket::make_connected_udp ( const EndPoint & end_point)
static

Makes a UDP socket connected to the given endpoint.

Parameters
end_pointThe endpoint that the socket is attempting to connect to.
Returns
The connected socket.

◆ 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.