xtd 0.2.0
Loading...
Searching...
No Matches

◆ accept_async()

bool xtd::net::sockets::socket::accept_async ( xtd::net::sockets::socket_async_event_args e)

Begins an asynchronous operation to accept an incoming connection attempt.

Parameters
eThe xtd::net::sockets::socket::socket_async_event_args object to use for this asynchronous socket operation.
Returns
true if the I/O operation is pending. The Completed event on the e parameter will be raised upon completion of the operation. false if the I/O operation completed synchronously. The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
Exceptions
xtd::net::sockets::socket_exceptionAn error occurred when attempting to access the socket.
xtd::object_closed_exceptionThe xtd::net::sockets::socket has been closed.
xtd::argument_exceptionAn argument is not valid. This exception occurs if the buffer provided is not large enough. The buffer must be at least 2 * (sizeof(SOCKADDR_STORAGE + 16) bytes. This exception also occurs if multiple buffers are specified, the xtd::net::sockets::socket_async_event_args::buffer_list property is not empty.
xtd::invalid_operation_exceptionThe accepting socket is not listening for connections. You must call xtd::net::sockets::socket::bind and xtd::net::sockets::socket::listen before calling xtd::net::sockets::socket::accept().
Remarks
Connection-oriented protocols can use the xtd::net::sockets::socket::accept_async method to asynchronously process incoming connection attempts. Accepting connections asynchronously gives you the ability to send and receive data within a separate execution thread. Before calling the xtd::net::sockets::socket::accept_async method, you must call the xtd::net::sockets::socket::listen method to listen for and queue incoming connection requests.
To be notified of completion, you must create a callback method that implements the xtd::delegate<const xtd::net::sockets::socket_async_event_args&> delegate and hook it to the xtd::net::sockets::socket_async_event_args::completed event.
The following properties and events on the xtd::net::sockets::socket_async_event_args object are required:
The caller can optionally specify an existing xtd::net::sockets::socket to use for the incoming connection by specifying the xtd::net::sockets::socket to use with the xtd::net::sockets::socket_async_eventArgs::accept_socket property.
If the xtd::net::sockets::socket_async_event_args::accept_socket property is empty, a new xtd::net::sockets::socket is constructed with the same xtd::net::sockets::address_family, xtd::net::sockets::socket_type, and xtd::net::sockets::protocol_type as the current xtd::net::sockets::socket and set as the xtd::net::sockets::socket_async_event_args::accept_socket property.
The caller may set the xtd::net::sockets::socket_async_event_args::user_token property to any user state object desired before calling the xtd::net::sockets::socket::accept_async method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.
Optionally, a buffer may be provided in which to receive the initial block of data on the socket after the xtd::net::sockets::socket::connect_async method succeeds. In this case, the xtd::net::sockets::socket_async_event_args::buffer property needs to be set to the buffer containing the data to receive and the xtd::net::sockets::socket_async_event_args::count property needs to be set to the maximum number of bytes of data to receive in the buffer. These properties can be set using the xtd::net::sockets::socket_async_event_args::set_buffer method. Part of the buffer passed in will be consumed internally for use by the underlying Winsock AcceptEx call. This means that the amount of data returned will always be less than the value of the xtd::net::sockets::socket::async_event_args::count property on the xtd::net::sockets::socket_async_event_args instance provided. The amount of the buffer used internally varies based on the address family of the socket. The minimum buffer size required is 288 bytes. If a larger buffer size is specified, then the xtd::net::sockets::socket will expect some extra data other than the address data received by the Winsock AcceptEx call and will wait until this extra data is received. If a timeout occurs, the connection is reset. So if extra data is expected of a specific amount, then the buffer size should be set to the minimum buffer size plus this amount.
The completion callback method should examine the xtd::net::sockets::socket_async_event_args::socket_error property to determine if the xtd::net::sockets::socket::accept_async operation was successful.
The xtd::net::sockets::socket_async_event_args::completed event can occur in some cases when no connection has been accepted and cause the xtd::net::sockets::socket_async_event_args::socket_error property to be set to xtd::net::sockets::socket_error::connection_reset. This can occur as a result of port scanning using a half-open SYN type scan (a SYN -> SYN-ACK -> RST sequence). Applications using the xtd::net::sockets::socket::accept_async method should be prepared to handle this condition.