Unix domain stream socket class.
Definition at line 10 of file UnixStreamSocket.h.
Take ownership of an existing native Unix stream 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 AF_UNIX SOCK_STREAM socket. On success ownership is transferred to the returned object and the descriptor will be closed when that object is destroyed.
An adopted socket does not claim ownership of its filesystem path.
- Parameters
-
| sockfd | native socket descriptor to adopt |
- Returns
- UnixStreamSocket owning sockfd
- Exceptions
-
| void NET::SimpleSocket::disconnect |
( |
| ) |
|
|
inherited |
Disconnect and unset any foreign addresses.
This will ungracefully abort the connection by sending an RST frame. disconnect() should not be used under normal circumstances, as it will terminate the connection OOB, preventing the other end of the communication from retrieving data that would still be available in the OS buffer.
For normal termination of a connection use shutdown().
- 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
-
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.