|
| | UDPSocket (AddressFamily family=IPv4) |
| IOResult | sendTo (const void *buffer, size_t len, std::string_view foreignAddress, unsigned short foreignPort) |
| IOResult | receiveFrom (void *buffer, size_t len, std::string &sourceAddress, unsigned short &sourcePort) |
| IOResult | timedReceiveFrom (void *buffer, size_t len, std::string &sourceAddress, unsigned short &sourcePort, int timeout) |
| void | setMulticastTTL (unsigned char multicastTTL) |
| void | setMulticastInterfaceAddr (const std::string &address) |
| void | joinGroup (const std::string &multicastGroup) |
| void | leaveGroup (const std::string &multicastGroup) |
| void | setReuseAddress (bool enable) |
| | Enable or disable local address reuse.
|
| ConnectResult | connect (std::string_view foreignAddress, unsigned short foreignPort) |
| | establish a connection with the given foreign address and port
|
| ConnectResult | timedConnect (std::string_view foreignAddress, unsigned short foreignPort, int timeout) |
| | establish a connection with the given foreign address and port
|
| void | bind (unsigned short localPort=0) |
| void | bind (std::string_view localAddress, unsigned short localPort=0) |
| | bind socket to address / port
|
| std::string | getLocalAddress () const |
| unsigned short | getLocalPort () const |
| std::string | getForeignAddress () const |
| unsigned short | getForeignPort () const |
| AddressFamily | addressFamily () const noexcept |
| | return wether this socket was created for IPv4 or IPv6
|
| int | nativeHandle () noexcept |
| | Return the native descriptor. Ownership remains with the object.
|
| int | duplicateNativeHandle () const |
| | Duplicates the underlaying native socket descriptor.
|
| bool | valid () const noexcept |
| | Returns true if socket is in a usable (non-moved) state and open.
|
| IOResult | send (const void *buffer, size_t len) |
| | send data through a connected socket
|
| IOResult | receive (void *buffer, size_t len) |
| | receive data from a bound socket
|
| IOResult | timedReceive (void *buffer, size_t len, int timeout) |
| | receive data from a bound socket, return after the given timespan
|
| void | disconnect () |
| | Disconnect and unset any foreign addresses.
|
| bool | isConnected () const |
| | check if the kernel currently has an associated peer address
|
| void | shutdown (ShutdownDirection type) |
| | shutdown the connection in the specified direction
|
| bool | peerDisconnected () const |
| | returns whether a currently associated peer disconnected
|
| bool | isBlocking () const |
| | returns true if socket is in blocking mode
|
| void | setBlocking (bool blocking) |
| | set socket to blocking or non-blocking I/O operations
|
| void | setSendBufferSize (int bytes) |
| | override default send buffer size for this socket
|
| void | setReceiveBufferSize (int bytes) |
| | override default receive buffer size for this socket
|
| UDPSocket NET::UDPSocket::adoptNativeHandle |
( |
int | sockfd | ) |
|
|
staticnodiscard |
Take ownership of an existing native UDP socket descriptor.
This is intended for sockets created outside simple-socket, for example listening sockets supplied through systemd socket activation.
The descriptor must refer to an IPv4 or IPv6 SCOK_DGRAM socket using the UDP protocol. On success ownership is transferred to the returned object and the descriptor will be closed when that object is destroyed. The address family IPv4 or IPv6 is detected automatically.
- Parameters
-
| sockfd | native socket descriptor to adopt |
- Returns
- TCPSocket owning sockfd
- Exceptions
-
| void NET::InternetSocket::bind |
( |
std::string_view | localAddress, |
|
|
unsigned short | localPort = 0 ) |
|
inherited |
bind socket to address / port
Set the local port to the specified port and the local address to an interface.
bind() attaches the local endpoint of the socket to the specified interface and port. Typically all servers use bind() to attach to the local port specified by the used protocol. You just need to call bind if you want to restrict the connection to a specific local interface, or if you want to listen on a specific port. If you are just a client using connect() to connect to a server, calling bind is most likely unnecessary.
To bind to a port < 1023 you need eighter superuser access, or the SUID bit set on your program.
- Parameters
-
| localAddress | local address |
| localPort | local port |
- Exceptions
-
| bool NET::SimpleSocket::isConnected |
( |
| ) |
const |
|
nodiscardinherited |
check if the kernel currently has an associated peer address
For stream sockets, true means a peer association exists; it does not guarantee that the remote endpoint is alive or that I/O can succeed. A peer that has sent FIN may still be associated with the socket.
For datagram sockets, true only indicates that connect() configured a default peer address; it does not establish a network connection.
- Returns
- false if getpeername() reports ENOTCONN
- Exceptions
-
| IOResult NET::SimpleSocket::receive |
( |
void * | buffer, |
|
|
size_t | len ) |
|
inherited |
receive data from a bound socket
receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.
- Parameters
-
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
- Returns
- IOResult
- Exceptions
-
| IOResult NET::SimpleSocket::send |
( |
const void * | buffer, |
|
|
size_t | len ) |
|
inherited |
send data through a connected socket
send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.
If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()
If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.
- Parameters
-
| buffer | data to be send |
| len | length of the data to be sent |
- Returns
- IOResult
- Exceptions
-
| IOResult NET::SimpleSocket::sendAll |
( |
const void * | buffer, |
|
|
size_t | len ) |
|
protectedinherited |
send data through a connected socket
sendAll() can only be used on a socket that called connect() before. If you try to use sendAll() on a unconnected socket, a SocketException will be thrown.
When using a stream oriented socket, the operating system is allowed to send only a part of the data you requested it to send. Because of this, you would have to check the number of sent bytes when using send(), and resend unsent data manually. sendAll() takes that responsibility from you and resends as long as it needs, to completely send the data given to it.
- Parameters
-
| buffer | data to be send |
| len | length of the data to be sent |
- Returns
- IOResult
- Exceptions
-
| void NET::SimpleSocket::setBlocking |
( |
bool | blocking | ) |
|
|
inherited |
set socket to blocking or non-blocking I/O operations
A non-blocking socket will return from all I/O operations right away. The IOStatus WouldBlock is used to signify no work can be done at the moment. An event handler can use poll(), epoll(), etc. to monitor the socket for readyness for the next operation.
All timed operations provided by socket classes are not affected. Timed calls wait up to timeout ms for even with non-blocking sockets.
shutdown the connection in the specified direction
This will gracefully end a connection by sending a FIN frame. Use this to end a normal communication cycle. To abort a connection see disconnect().
Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.
If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.
- Parameters
-
- Exceptions
-
- Examples
- TCP_Server.cpp.
| IOResult NET::SimpleSocket::timedReceive |
( |
void * | buffer, |
|
|
size_t | len, |
|
|
int | timeout ) |
|
inherited |
receive data from a bound socket, return after the given timespan
timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.
- Parameters
-
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| timeout | the timeout in ms after which receive will give up and return |
- Returns
- IOResult
- Exceptions
-
- Examples
- TCP_Client.cpp, and TCP_Client_Reconnect.cpp.