System.Net.Sockets.Socket.ConnectAsync Method

Begins an asynchronous request for a connection to a remote host.

Syntax

public bool ConnectAsync (SocketAsyncEventArgs e)

Parameters

e
The System.Net.Sockets.SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the e parameter will be raised upon completion of the operation.

Returns false if the I/O operation completed synchronously. In this case, The SocketAsyncEventArgs.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.

Remarks

If you are using a connection-oriented protocol, the Socket.ConnectAsync(SocketAsyncEventArgs) method starts an asynchronous request for a connection to the remote host. If you are using a connectionless protocol, Socket.ConnectAsync(SocketAsyncEventArgs) establishes a default remote host.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the System.Net.IPEndPoint of the remote host to connect to.

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the Socket.ConnectAsync(SocketAsyncEventArgs) 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.

If you are using a connectionless protocol such as UDP, you do not have to call Socket.ConnectAsync(SocketAsyncEventArgs) before sending and receiving data. You can use Socket.SendToAsync(SocketAsyncEventArgs) and Socket.ReceiveFromAsync(SocketAsyncEventArgs) to communicate with a remote host. If you do call Socket.ConnectAsync(SocketAsyncEventArgs), any datagrams that arrive from an address other than the specified default will be discarded. If you want to change the default remote host, call the Socket.ConnectAsync(SocketAsyncEventArgs) method again with the desired endpoint.

If you wish to set the default remote host to a broadcast address, you must first call Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, bool) and set Broadcast to true. If this is not done, the Socket.ConnectAsync(SocketAsyncEventArgs) method will throw a System.Net.Sockets.SocketException.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required:

Optionally, a buffer may be provided which will atomically be sent on the socket after the Socket.ConnectAsync(SocketAsyncEventArgs) method succeeds. In this case, the SocketAsyncEventArgs.Buffer property needs to be set to the buffer containing the data to send and the SocketAsyncEventArgs.Count property needs to be set to the number of bytes of data to send from the buffer. Once a connection is established, this buffer of data is sent.

If you are using a connection-oriented protocol and do not call Socket.Bind(System.Net.EndPoint) before calling Socket.ConnectAsync(SocketAsyncEventArgs), 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 IP address and port number until you call the Socket.SendAsync(SocketAsyncEventArgs) or Socket.ReceiveAsync(SocketAsyncEventArgs) methods.

The Socket.ConnectAsync(SocketAsyncEventArgs) method throws NotSupportedException if the address family of the System.Net.Sockets.Socket and the SocketAsyncEventArgs.RemoteEndPoint are not the same address family.

Note:

If you receive a System.Net.Sockets.SocketException when calling this method, 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.

Requirements

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