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

◆ begin_accept()

xtd::sptr< xtd::iasync_result > xtd::net::sockets::socket::begin_accept ( xtd::async_callback  callback,
const std::any &  state 
)

Begins an asynchronous operation to accept an incoming connection attempt.

Parameters
callbackThe xtd::async_callback delegate.
stateAn object that contains state information for this request.
Returns
An xtd::iasync_result that references the asynchronous connection.
Exceptions
xtd::object_closed_exceptionThe xtd::net::sockets::socket has been closed.
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().
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::begin_accept 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::begin_accept method, you must call the xtd::net::sockets::socket::socket::listen method to listen for and queue incoming connection requests.
You must create a callback method that implements the xtd::async_callback delegate and pass its name to the xtd::net::sockets::socket::begin_accept method. To do this, at the very minimum, you must pass the listening xtd::net::sockets::socket::socket object to xtd::net::sockets::socket::begin_accept through the state parameter. If your callback needs more information, you can create a small class to hold the xtd::net::sockets::socket::socket and the other required information. Pass an instance of this class to the xtd::net::sockets::socket::begin_accept method through the state parameter.
Your callback method should invoke the xtd::net::sockets::socket::end_accept method. When your application calls xtd::net::sockets::socket::begin_accept, the system usually uses a separate thread to execute the specified callback method and blocks on xtd::net::sockets::socket::end_accept until a pending connection is retrieved. xtd::net::sockets::socket::end_accept will return a new xtd::net::sockets::socket::socket object that you can use to send and receive data with the remote host. You cannot use this returned xtd::net::sockets::socket::socket to accept any additional connections from the connection queue. If you want the original thread to block after you call the xtd::net::sockets::socket::begin_accept method, use xtd::threading::mutex::wait_one. Call the xtd::threading::mutex::release_mutex method on a xtd::threading::mutex in the callback method when you want the original thread to continue executing.
The system may also use the calling thread to invoke the callback method. In this case, the xtd::iasync_result::completed_synchronously property on the returned xtd::iasync_result will be set to indicate that the xtd::net::sockets::socket::begin_accept method completed synchronously.
To cancel a pending call to the xtd::net::sockets::socket::begin_accept method, close the xtd::net::sockets::socket::socket. When the xtd::sockets::socket::close method is called while an asynchronous operation is in progress, the callback provided to the xtd::net::sockets::socket::begin_accept method is called. A subsequent call to the xtd::net::sockets::socket::end_accept method will throw an xtd::object_closed_exception to indicate that the operation has been cancelled.
Note
You can use the xtd::net::sockets::socket::remote_end_point property of the returned xtd::net::sockets::socket::socket to identify the remote host's network address and port number.
If you receive a xtd::net::sockets::socket_exception exception, use the xtd::net::sockets::socket_exception::error_code property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.