Socket.ConnectAsync Method

Definition

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

Overloads

ConnectAsync(SocketType, ProtocolType, SocketAsyncEventArgs)

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

ConnectAsync(IPAddress[], Int32, CancellationToken)

Establishes a connection to a remote host.

ConnectAsync(String, Int32, CancellationToken)

Establishes a connection to a remote host.

ConnectAsync(IPAddress, Int32, CancellationToken)

Establishes a connection to a remote host.

ConnectAsync(String, Int32)

Establishes a connection to a remote host.

ConnectAsync(IPAddress[], Int32)

Establishes a connection to a remote host.

ConnectAsync(IPAddress, Int32)

Establishes a connection to a remote host.

ConnectAsync(EndPoint, CancellationToken)

Establishes a connection to a remote host.

ConnectAsync(SocketAsyncEventArgs)

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

ConnectAsync(EndPoint)

Establishes a connection to a remote host.

ConnectAsync(SocketType, ProtocolType, SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

C#
public static bool ConnectAsync (System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.Sockets.SocketAsyncEventArgs e);

Parameters

socketType
SocketType

One of the SocketType values.

protocolType
ProtocolType

One of the ProtocolType values.

e
SocketAsyncEventArgs

The SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

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

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

Exceptions

An argument is not valid. This exception occurs if multiple buffers are specified, the BufferList property is not null.

The e parameter cannot be null and the RemoteEndPoint cannot be null.

The Socket is listening or a socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

An error occurred when attempting to access the socket.

The local endpoint and the RemoteEndPoint are not the same address family.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

If you are using a connection-oriented protocol, the M:System.Net.Sockets.Socket.ConnectAsync(System.Net.Sockets.SocketType,System.Net.Sockets.ProtocolType,System.Net.Sockets.SocketAsyncEventArgs) method starts an asynchronous request for a connection to the remote host. If you are using a connectionless protocol, ConnectAsync establishes a default remote host specified by the socketType and protocolType parameters.

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 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 ConnectAsync 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 ConnectAsync before sending and receiving data. You can use SendToAsync and ReceiveFromAsync to communicate with a remote host. If you do call ConnectAsync, 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 ConnectAsync method again with the desired endpoint.

If you wish to set the default remote host to a broadcast address, you must first call SetSocketOption and set Broadcast to true. If this is not done, the ConnectAsync method will throw a 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 ConnectAsync 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 Bind before calling ConnectAsync, 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 SendAsync or ReceiveAsync methods.

The ConnectAsync method throws NotSupportedException if the address family of the Socket and the SocketAsyncEventArgs.RemoteEndPoint are not the same address family.

Note

If you receive a 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 for a detailed description of the error.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ConnectAsync(IPAddress[], Int32, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.IPAddress[] addresses, int port, System.Threading.CancellationToken cancellationToken);

Parameters

addresses
IPAddress[]

A list of IPAddresses for the remote host that will be used to attempt to connect to the remote host.

port
Int32

The port on the remote host to connect to.

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The addresses parameter cannot be null.

The addresses parameter cannot be empty array.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

The socket is not in the InterNetwork or InterNetworkV6 families.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(IPAddress[], Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(String, Int32, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.ValueTask ConnectAsync (string host, int port, System.Threading.CancellationToken cancellationToken);

Parameters

host
String

The hostname of the remote host to connect to.

port
Int32

The port on the remote host to connect to.

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The host parameter cannot be null.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

The socket is not in the InterNetwork or InterNetworkV6 families.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(String, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(IPAddress, Int32, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.IPAddress address, int port, System.Threading.CancellationToken cancellationToken);

Parameters

address
IPAddress

The IPAddress of the remote host to connect to.

port
Int32

The port on the remote host to connect to.

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The address parameter cannot be null.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(IPAddress, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(String, Int32)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.Task ConnectAsync (string host, int port);

Parameters

host
String

The hostname of the remote host to connect to.

port
Int32

The port on the remote host to connect to.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The host parameter cannot be null.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

The socket is not in the InterNetwork or InterNetworkV6 families.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(String, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(IPAddress[], Int32)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.Task ConnectAsync (System.Net.IPAddress[] addresses, int port);

Parameters

addresses
IPAddress[]

A list of IPAddresses for the remote host that will be used to attempt to connect to the remote host.

port
Int32

The port on the remote host to connect to.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The addresses parameter cannot be null.

The addresses parameter cannot be empty array.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

The socket is not in the InterNetwork or InterNetworkV6 families.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(IPAddress[], Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(IPAddress, Int32)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.Task ConnectAsync (System.Net.IPAddress address, int port);

Parameters

address
IPAddress

The IPAddress of the remote host to connect to.

port
Int32

The port on the remote host to connect to.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The address parameter cannot be null.

port is less than MinPort.

-or-

port is greater than MaxPort.

The Socket is listening.

The socket is not in the InterNetwork or InterNetworkV6 families.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(IPAddress, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(EndPoint, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.EndPoint remoteEP, System.Threading.CancellationToken cancellationToken);

Parameters

remoteEP
EndPoint

The endpoint to connect to.

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The remoteEP parameter cannot be null.

The Socket is listening.

An error occurred when attempting to access the socket.

The local endpoint and the remoteEP parameter are not the same address family.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(EndPoint).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

ConnectAsync(SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

C#
public bool ConnectAsync (System.Net.Sockets.SocketAsyncEventArgs e);

Parameters

e
SocketAsyncEventArgs

The SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

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

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

Exceptions

An argument is not valid. This exception occurs if multiple buffers are specified, the BufferList property is not null.

The e parameter cannot be null and the RemoteEndPoint cannot be null.

The Socket is listening or a socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

An error occurred when attempting to access the socket.

The local endpoint and the RemoteEndPoint are not the same address family.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

If you are using a connection-oriented protocol, the ConnectAsync method starts an asynchronous request for a connection to the remote host. If you are using a connectionless protocol, ConnectAsync 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 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 ConnectAsync 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 ConnectAsync before sending and receiving data. You can use SendToAsync and ReceiveFromAsync to communicate with a remote host. If you do call ConnectAsync, 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 ConnectAsync method again with the desired endpoint.

If you wish to set the default remote host to a broadcast address, you must first call SetSocketOption and set Broadcast to true. If this is not done, the ConnectAsync method will throw a 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 ConnectAsync 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 Bind before calling ConnectAsync, 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 SendAsync or ReceiveAsync methods.

The ConnectAsync method throws NotSupportedException if the address family of the Socket and the SocketAsyncEventArgs.RemoteEndPoint are not the same address family.

Note

If you receive a 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 for a detailed description of the error.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ConnectAsync(EndPoint)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Establishes a connection to a remote host.

C#
public System.Threading.Tasks.Task ConnectAsync (System.Net.EndPoint remoteEP);

Parameters

remoteEP
EndPoint

The endpoint to connect to.

Returns

An asynchronous task that completes when the connection is established.

Exceptions

The remoteEP parameter cannot be null.

The Socket is listening.

An error occurred when attempting to access the socket.

The local endpoint and the remoteEP parameter are not the same address family.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Connect(EndPoint).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9