Network socket (POSIX).
More...
#include <socket.hpp>
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
◆ 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
-
◆ Socket() [3/5]
Socket constructor: open a new socket
- Parameters
-
| family | the address family (AF_INET or AF_INET6 ) |
| type | the socket type |
- Exceptions
-
◆ Socket() [4/5]
| anch::network::Socket::Socket |
( |
const Socket & | | ) |
|
|
delete |
Forbids Socket copy constructor
◆ Socket() [5/5]
| anch::network::Socket::Socket |
( |
Socket && | other | ) |
|
|
noexcept |
Socket move constructor
- Parameters
-
| other | the socket to move (it is closed afterwards) |
◆ ~Socket()
| virtual anch::network::Socket::~Socket |
( |
| ) |
|
|
virtualnoexcept |
Socket destructor: close the socket
◆ accept()
Wait for a connection on a listening socket
- Parameters
-
| peer | if not NULL , receives the peer address |
- Returns
- the connected socket
- Exceptions
-
◆ bind()
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
-
| host | the local host name or numeric address (empty for any address) |
| port | the local port (0 for an ephemeral port: see getLocalAddress ) |
| type | the socket type |
- Returns
- the bound socket
- Exceptions
-
◆ close()
| void anch::network::Socket::close |
( |
| ) |
|
|
noexcept |
Close the socket (do nothing when already closed)
◆ connect()
Open a socket connected to remote host. Every resolved address is tried (IPv4 and IPv6).
- Parameters
-
| host | the remote host name or numeric address (empty for loopback) |
| port | the remote port |
| type | the socket type |
- Returns
- the connected socket
- Exceptions
-
◆ getDescriptor()
| int anch::network::Socket::getDescriptor |
( |
| ) |
const |
|
noexcept |
File descriptor getter
- Returns
- the file descriptor (
-1 when closed)
◆ getLocalAddress()
Local address getter
- Returns
- the address the socket is bound to
- Exceptions
-
◆ getPeerAddress()
Peer address getter
- Returns
- the address of the connected peer
- Exceptions
-
◆ isOpen()
| bool anch::network::Socket::isOpen |
( |
| ) |
const |
|
noexcept |
Check if socket is opened
- Returns
true when socket is opened, false otherwise
◆ listen()
| void anch::network::Socket::listen |
( |
int | backlog = SOMAXCONN | ) |
|
Listen for connections on a bound socket
- Parameters
-
| backlog | the maximum number of pending connections |
- Exceptions
-
◆ operator=() [1/2]
| Socket & anch::network::Socket::operator= |
( |
const Socket & | | ) |
|
|
delete |
Forbids Socket copy assignment
◆ operator=() [2/2]
| Socket & anch::network::Socket::operator= |
( |
Socket && | other | ) |
|
|
noexcept |
Socket move assignment (the current socket is closed)
- Parameters
-
| other | the socket to move (it is closed afterwards) |
- Returns
this
◆ receive()
| std::size_t anch::network::Socket::receive |
( |
char * | data, |
|
|
std::size_t | size ) |
Receive data
- Parameters
-
| data | the buffer |
| size | the buffer size |
- Returns
- the number of bytes received,
0 when peer has closed the connection
- Exceptions
-
◆ receiveFrom()
| std::size_t anch::network::Socket::receiveFrom |
( |
char * | data, |
|
|
std::size_t | size, |
|
|
anch::network::Address & | from ) |
Receive a datagram
- Parameters
-
| data | the buffer |
| size | the buffer size |
| from | receives the sender address |
- Returns
- the datagram size
- Exceptions
-
◆ 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
-
| data | the data |
| size | the data size |
- Returns
- the number of bytes sent
- Exceptions
-
◆ sendAll()
| void anch::network::Socket::sendAll |
( |
const char * | data, |
|
|
std::size_t | size ) |
Send every data (loop on partial sends)
- Parameters
-
| data | the data |
| size | the data size |
- Exceptions
-
◆ sendTo()
| std::size_t anch::network::Socket::sendTo |
( |
const char * | data, |
|
|
std::size_t | size, |
|
|
const anch::network::Address & | to ) |
Send a datagram
- Parameters
-
| data | the data |
| size | the data size |
| to | the destination address |
- Returns
- the number of bytes sent
- Exceptions
-
◆ setReceiveTimeout()
| void anch::network::Socket::setReceiveTimeout |
( |
std::chrono::milliseconds | timeout | ) |
|
◆ setSendTimeout()
| void anch::network::Socket::setSendTimeout |
( |
std::chrono::milliseconds | timeout | ) |
|
Set send timeout
- Parameters
-
| timeout | the timeout (0 for no timeout) |
- Exceptions
-
◆ shutdown()
Shut down part of a full-duplex connection
- Parameters
-
| how | the direction to shut down |
- Exceptions
-
The documentation for this class was generated from the following file: