Socket.BeginConnect Method (String, Int32, AsyncCallback, Object)
Begins an asynchronous request for a remote host connection. The host is specified by a host name and a port number.
Namespace: System.Net.Sockets
Assembly: System (in System.dll)
'Declaration <HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)> _ Public Function BeginConnect ( _ host As String, _ port As Integer, _ requestCallback As AsyncCallback, _ state As Object _ ) As IAsyncResult
Parameters
- host
- Type: System.String
The name of the remote host.
- port
- Type: System.Int32
The port number of the remote host.
- requestCallback
- Type: System.AsyncCallback
An AsyncCallback delegate that references the method to invoke when the connect operation is complete.
- state
- Type: System.Object
A user-defined object that contains information about the connect operation. This object is passed to the requestCallback delegate when the operation is complete.
| Exception | Condition |
|---|---|
| ArgumentNullException | host is Nothing. |
| ObjectDisposedException | The Socket has been closed. |
| NotSupportedException | This method is valid for sockets in the InterNetwork or InterNetworkV6 families. |
| ArgumentOutOfRangeException | The port number is not valid. |
| InvalidOperationException |
The asynchronous BeginConnect operation must be completed by calling the EndConnect method. Typically, the method is invoked by the requestCallback delegate.
This method does not block until the operation is complete. To block until the operation is complete, use one of the Connect method overloads.
To cancel a pending call to the BeginConnect method, close the Socket. When the Close method is called while an asynchronous operation is in progress, the callback provided to the BeginConnect method is called. A subsequent call to the EndConnect method will throw an ObjectDisposedException to indicate that the operation has been cancelled.
For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously
Note |
|---|
If you receive a SocketException, use the SocketException.ErrorCode 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. |
Note |
|---|
If this socket has previously been disconnected, then BeginConnect 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 EndPoint that is used must be different. |
Note |
|---|
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing. |
Note |
|---|
The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement. |
Note |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note