AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::network::Socket Class Reference

Network socket (POSIX). More...

#include <socket.hpp>

Collaboration diagram for anch::network::Socket:

Public Member Functions

 Socket () noexcept
 Socket (int fd) noexcept
 Socket (int family, anch::network::SocketType type)
 Socket (const Socket &)=delete
 Socket (Socket &&other) noexcept
virtual ~Socket () noexcept
Socket & operator= (const Socket &)=delete
Socket & operator= (Socket &&other) noexcept
void listen (int backlog=SOMAXCONN)
Socket accept (anch::network::Address *peer=NULL)
std::size_t send (const char *data, std::size_t size)
void sendAll (const char *data, std::size_t size)
std::size_t receive (char *data, std::size_t size)
std::size_t sendTo (const char *data, std::size_t size, const anch::network::Address &to)
std::size_t receiveFrom (char *data, std::size_t size, anch::network::Address &from)
void shutdown (anch::network::Direction how=anch::network::Direction::BOTH)
void close () noexcept
void setReceiveTimeout (std::chrono::milliseconds timeout)
void setSendTimeout (std::chrono::milliseconds timeout)
anch::network::Address getLocalAddress () const
anch::network::Address getPeerAddress () const
int getDescriptor () const noexcept
bool isOpen () const noexcept

Static Public Member Functions

static Socket connect (const std::string &host, uint16_t port, anch::network::SocketType type)
static Socket bind (const std::string &host, uint16_t port, anch::network::SocketType type)

Detailed Description

Network socket (POSIX).

A Socket owns its file descriptor: it is closed by the destructor. Sockets can be moved, not copied.
Every method raises IOException with the errno value on error. Interrupted system calls (EINTR ) are restarted. SIGPIPE is not raised when peer has closed the connection: send raises IOException (EPIPE ) instead.
For TCP connections, TcpStream and TcpServer provide a stream interface ; for UDP, UdpSocket .

Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ Socket() [1/5]

anch::network::Socket::Socket ( )
noexcept

Socket default constructor (closed socket)

◆ Socket() [2/5]

anch::network::Socket::Socket ( int fd)
explicitnoexcept

Socket constructor: take ownership of an opened file descriptor

Parameters
fdthe file descriptor
Here is the call graph for this function:

◆ Socket() [3/5]

anch::network::Socket::Socket ( int family,
anch::network::SocketType type )

Socket constructor: open a new socket

Parameters
familythe address family (AF_INET or AF_INET6 )
typethe socket type
Exceptions
anch::network::IOExceptionsocket can not be opened
Here is the call graph for this function:

◆ Socket() [4/5]

anch::network::Socket::Socket ( const Socket & )
delete

Forbids Socket copy constructor

Here is the call graph for this function:

◆ Socket() [5/5]

anch::network::Socket::Socket ( Socket && other)
noexcept

Socket move constructor

Parameters
otherthe socket to move (it is closed afterwards)
Here is the call graph for this function:

◆ ~Socket()

virtual anch::network::Socket::~Socket ( )
virtualnoexcept

Socket destructor: close the socket

Here is the call graph for this function:

Member Function Documentation

◆ accept()

Socket anch::network::Socket::accept ( anch::network::Address * peer = NULL)

Wait for a connection on a listening socket

Parameters
peerif not NULL , receives the peer address
Returns
the connected socket
Exceptions
anch::network::IOExceptionerror on accept
Here is the call graph for this function:

◆ bind()

Socket anch::network::Socket::bind ( const std::string & host,
uint16_t port,
anch::network::SocketType type )
static

Open a socket bound to a local address, with SO_REUSEADDR . Every resolved address is tried. An IPv6 socket bound to any address also accepts IPv4 connections.

Parameters
hostthe local host name or numeric address (empty for any address)
portthe local port (0 for an ephemeral port: see getLocalAddress )
typethe socket type
Returns
the bound socket
Exceptions
anch::network::IOExceptionresolution or bind error (error of the last address)
Here is the call graph for this function:

◆ close()

void anch::network::Socket::close ( )
noexcept

Close the socket (do nothing when already closed)

Here is the call graph for this function:

◆ connect()

Socket anch::network::Socket::connect ( const std::string & host,
uint16_t port,
anch::network::SocketType type )
static

Open a socket connected to remote host. Every resolved address is tried (IPv4 and IPv6).

Parameters
hostthe remote host name or numeric address (empty for loopback)
portthe remote port
typethe socket type
Returns
the connected socket
Exceptions
anch::network::IOExceptionresolution or connection error (error of the last address)
Here is the call graph for this function:

◆ getDescriptor()

int anch::network::Socket::getDescriptor ( ) const
noexcept

File descriptor getter

Returns
the file descriptor (-1 when closed)
Here is the call graph for this function:

◆ getLocalAddress()

anch::network::Address anch::network::Socket::getLocalAddress ( ) const

Local address getter

Returns
the address the socket is bound to
Exceptions
anch::network::IOExceptionerror on getsockname
Here is the call graph for this function:

◆ getPeerAddress()

anch::network::Address anch::network::Socket::getPeerAddress ( ) const

Peer address getter

Returns
the address of the connected peer
Exceptions
anch::network::IOExceptionerror on getpeername
Here is the call graph for this function:

◆ isOpen()

bool anch::network::Socket::isOpen ( ) const
noexcept

Check if socket is opened

Returns
true when socket is opened, false otherwise
Here is the call graph for this function:

◆ listen()

void anch::network::Socket::listen ( int backlog = SOMAXCONN)

Listen for connections on a bound socket

Parameters
backlogthe maximum number of pending connections
Exceptions
anch::network::IOExceptionerror on listen
Here is the call graph for this function:

◆ operator=() [1/2]

Socket & anch::network::Socket::operator= ( const Socket & )
delete

Forbids Socket copy assignment

Here is the call graph for this function:

◆ operator=() [2/2]

Socket & anch::network::Socket::operator= ( Socket && other)
noexcept

Socket move assignment (the current socket is closed)

Parameters
otherthe socket to move (it is closed afterwards)
Returns
this
Here is the call graph for this function:

◆ receive()

std::size_t anch::network::Socket::receive ( char * data,
std::size_t size )

Receive data

Parameters
datathe buffer
sizethe buffer size
Returns
the number of bytes received, 0 when peer has closed the connection
Exceptions
anch::network::IOExceptionerror on recv (EAGAIN on timeout)
Here is the call graph for this function:

◆ receiveFrom()

std::size_t anch::network::Socket::receiveFrom ( char * data,
std::size_t size,
anch::network::Address & from )

Receive a datagram

Parameters
datathe buffer
sizethe buffer size
fromreceives the sender address
Returns
the datagram size
Exceptions
anch::network::IOExceptionerror on recvmsg , or datagram larger than buffer (EMSGSIZE )
Here is the call graph for this function:

◆ send()

std::size_t anch::network::Socket::send ( const char * data,
std::size_t size )

Send data (one system call: less bytes than size can be sent)

Parameters
datathe data
sizethe data size
Returns
the number of bytes sent
Exceptions
anch::network::IOExceptionerror on send
Here is the call graph for this function:

◆ sendAll()

void anch::network::Socket::sendAll ( const char * data,
std::size_t size )

Send every data (loop on partial sends)

Parameters
datathe data
sizethe data size
Exceptions
anch::network::IOExceptionerror on send
Here is the call graph for this function:

◆ sendTo()

std::size_t anch::network::Socket::sendTo ( const char * data,
std::size_t size,
const anch::network::Address & to )

Send a datagram

Parameters
datathe data
sizethe data size
tothe destination address
Returns
the number of bytes sent
Exceptions
anch::network::IOExceptionerror on sendto
Here is the call graph for this function:

◆ setReceiveTimeout()

void anch::network::Socket::setReceiveTimeout ( std::chrono::milliseconds timeout)

Set receive timeout (receive and receiveFrom raise IOException with EAGAIN )

Parameters
timeoutthe timeout (0 for no timeout)
Exceptions
anch::network::IOExceptionerror on setsockopt
Here is the call graph for this function:

◆ setSendTimeout()

void anch::network::Socket::setSendTimeout ( std::chrono::milliseconds timeout)

Set send timeout

Parameters
timeoutthe timeout (0 for no timeout)
Exceptions
anch::network::IOExceptionerror on setsockopt
Here is the call graph for this function:

◆ shutdown()

void anch::network::Socket::shutdown ( anch::network::Direction how = anch::network::Direction::BOTH)

Shut down part of a full-duplex connection

Parameters
howthe direction to shut down
Exceptions
anch::network::IOExceptionerror on shutdown
Here is the call graph for this function:

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