System.Net.Sockets.Socket.BeginConnect Method

Begins an asynchronous request for a remote host connection.

Syntax

public IAsyncResult BeginConnect (System.Net.EndPoint end_point, AsyncCallback callback, object state)

Parameters

end_point
Documentation for this section has not yet been entered.
callback
The AsyncCallback delegate.
state
An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous connection.

Exceptions

TypeReason
ArgumentNullException remoteEP is null.
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
ObjectDisposedExceptionThe current instance has been disposed.
System.Security.SecurityExceptionA caller higher in the call stack does not have permission for the requested operation.

Remarks

If you are using a connection-oriented protocol, the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method starts an asynchronous request for a connection to the remoteEP parameter. If you are using a connectionless protocol, Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) establishes a default remote host. Connecting or setting the default remote host asynchronously gives you the ability to send and receive data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method. At the very minimum, you must pass the System.Net.Sockets.Socket to Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) through the state parameter. If your callback needs more information, you can create a small class to hold the System.Net.Sockets.Socket, and the other required information. Pass an instance of this class to the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method through the state parameter.

Your callback method should invoke the Socket.EndConnect(IAsyncResult) method. When your application calls Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), the system will use a separate thread to execute the specified callback method, and will block on Socket.EndConnect(IAsyncResult) until the System.Net.Sockets.Socket connects successfully or throws an exception. If you want the original thread to block after you call the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method, use System.Threading.WaitHandle.WaitOne(int, bool). Call the Set method on a System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see [<topic://cpconcallbacksample>].

If you are using a connectionless protocol such as UDP, you do not have to call Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) before sending and receiving data. You can use Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) and Socket.BeginReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@, AsyncCallback, object) to communicate with a remote host. If you do call Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), any datagrams that arrive from an address other than the specified default will be discarded. If you wish to set your default remote host to a broadcast address, you must first call Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) and set Broadcast to true. If you cannot, Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) will throw a System.Net.Sockets.SocketException.

If you are using a connection-oriented protocol and do not call Socket.Bind(System.Net.EndPoint) before calling Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), the underlying service provider will assign the most appropriate local network address and port number. If you are using a connectionless protocol, the service provider will not assign a local network address and port number until you call the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) or Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) method. If you want to change the default remote host, call the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method again with the desired endpoint.

To cancel a pending call to the erload:System.Net.Sockets.Socket.BeginConnect method, close the System.Net.Sockets.Socket. When the Socket.Close method is called while an asynchronous operation is in progress, the callback provided to the erload:System.Net.Sockets.Socket.BeginConnect method is called. A subsequent call to the Socket.EndConnect(IAsyncResult) method will throw an ObjectDisposedException to indicate that the operation has been cancelled.

Note:

If you receive a System.Net.Sockets.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 erload:System.Net.Sockets.Socket.BeginConnect must be called on a thread that will not exit until the operation is complete. This is a limitation of the underlying provider.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous System.Net.Sockets.Socket methods. After the first use of a particular context (a specific asynchronous System.Net.Sockets.Socket method, a specific System.Net.Sockets.Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

Permissions

TypeReason
System.Net.SocketPermissionRequires permission to make a connection to the endpoint defined by remoteEP.

Example

For an outline of an asynchronous operation, see the Socket.BeginAccept(AsyncCallback, object) method. For the complete example, which uses the Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method, see the System.Net.Sockets.Socket class overview.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0