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

◆ begin_connect() [2/4]

xtd::sptr< xtd::iasync_result > xtd::net::sockets::socket::begin_connect ( const xtd::net::ip_address address,
uint16  port,
xtd::async_callback  callback,
const std::any &  state 
)

Begins an asynchronous request for a remote host connection. The host is specified by an xtd::net::ip_address and a port number.

Parameters
addressThe xtd::net::ip_address of the remote host.
portThe port number of the remote host.
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().
Remarks
The asynchronous xtd::net::sockets::socket::begin_connect operation must be completed by calling the xtd::net::sockets::socket::end_connect method. Typically, the method is invoked by the callback delegate.
This method does not block until the operation is complete. To block until the operation is complete, use one of the xtd::net::sockets::socket::connect method overloads, or xtd::net::sockets::socket::end_connect.
To cancel a pending call to the xtd::net::sockets::socket::begin_connect method, close the xtd::net::sockets::socket::socket. When the xtd::net::sockets::socket::socket::close method is called while an asynchronous operation is in progress, the callback provided to the xtd::net::sockets::socket::begin_connect method is called. A subsequent call to the xtd::net::sockets::socket::end_connect method will throw an xtd::object_closed_exception to indicate that the operation has been cancelled.
Note
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.
If this socket has previously been disconnected, then xtd::net::sockets::socket::begin_connect must be called on a thread that will not exit until the operation is complete. This is a limitation of the underlying provider. Also the xtd::net::end_point that is used must be different.