Redigera

Dela via


TcpClient.ConnectAsync Method

Definition

Connects the client to a remote TCP host using the specified host name and port number as an asynchronous operation.

Overloads

ConnectAsync(String, Int32, CancellationToken)

Connects the client to the specified TCP port on the specified host as an asynchronous operation.

ConnectAsync(IPAddress[], Int32, CancellationToken)

Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

ConnectAsync(IPAddress, Int32, CancellationToken)

Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

ConnectAsync(String, Int32)

Connects the client to the specified TCP port on the specified host as an asynchronous operation.

ConnectAsync(IPEndPoint, CancellationToken)

Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.

ConnectAsync(IPAddress[], Int32)

Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

ConnectAsync(IPAddress, Int32)

Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

ConnectAsync(IPEndPoint)

Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.

ConnectAsync(String, Int32, CancellationToken)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to the specified TCP port on the specified host as an asynchronous operation.

public:
 System::Threading::Tasks::ValueTask ConnectAsync(System::String ^ host, int port, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask ConnectAsync (string host, int port, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : string * int * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Function ConnectAsync (host As String, port As Integer, cancellationToken As CancellationToken) As ValueTask

Parameters

host
String

The DNS name of the remote host.

port
Int32

The port number of the remote host.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

A task that represents the asynchronous connection operation.

Exceptions

The host parameter is null.

The port parameter is not between MinPort and MaxPort.

An error occurred when accessing the socket.

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

Remarks

This operation will not block. The returned task will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified host name and port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

If IPv6 is enabled and the ConnectAsync(String, Int32) method is called to connect to a host that resolves to both IPv6 and IPv4 addresses, the connection to the IPv6 address will be attempted first before the IPv4 address. This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

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).

See also

Applies to

ConnectAsync(IPAddress[], Int32, CancellationToken)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

public:
 System::Threading::Tasks::ValueTask ConnectAsync(cli::array <System::Net::IPAddress ^> ^ addresses, int port, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.IPAddress[] addresses, int port, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : System.Net.IPAddress[] * int * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Function ConnectAsync (addresses As IPAddress(), port As Integer, cancellationToken As CancellationToken) As ValueTask

Parameters

addresses
IPAddress[]

The array of IP address of the remote host.

port
Int32

The port number of the remote host.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

A task that represents the asynchronous connection operation.

Exceptions

The ipAddresses parameter is null.

The port number is not valid.

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.

This method is valid for sockets that use the InterNetwork flag or the InterNetworkV6 flag.

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

Remarks

This operation will not block. The returned task will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. Call this method to establish a synchronous remote host connection to the host specified by the array of IP addresses and the port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive NotSupportedException with message This protocol version is not supported while using IPv6 address, then make sure you enabled IPv6 in constructor by passing InterNetworkV6.

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).

See also

Applies to

ConnectAsync(IPAddress, Int32, CancellationToken)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

public:
 System::Threading::Tasks::ValueTask ConnectAsync(System::Net::IPAddress ^ address, int port, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.IPAddress address, int port, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : System.Net.IPAddress * int * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Function ConnectAsync (address As IPAddress, port As Integer, cancellationToken As CancellationToken) As ValueTask

Parameters

address
IPAddress

The IP address of the remote host.

port
Int32

The port number of the remote host.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

A task that represents the asynchronous connection operation.

Exceptions

The address parameter is null.

The port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

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

Remarks

This operation will not block. The returned task will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified IP address and port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive NotSupportedException with message This protocol version is not supported while using IPv6 address, then make sure you enabled IPv6 in constructor by passing InterNetworkV6.

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).

See also

Applies to

ConnectAsync(String, Int32)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to the specified TCP port on the specified host as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ ConnectAsync(System::String ^ host, int port);
public System.Threading.Tasks.Task ConnectAsync (string host, int port);
member this.ConnectAsync : string * int -> System.Threading.Tasks.Task
Public Function ConnectAsync (host As String, port As Integer) As Task

Parameters

host
String

The DNS name of the remote host to which you intend to connect.

port
Int32

The port number of the remote host to which you intend to connect.

Returns

The task object representing the asynchronous operation.

Exceptions

The host parameter is null.

The port parameter is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Remarks

This operation will not block. The returned Returns Task object will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified host name and port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

If IPv6 is enabled and the ConnectAsync(String, Int32) method is called to connect to a host that resolves to both IPv6 and IPv4 addresses, the connection to the IPv6 address will be attempted first before the IPv4 address. This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

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).

See also

Applies to

ConnectAsync(IPEndPoint, CancellationToken)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.

public:
 System::Threading::Tasks::ValueTask ConnectAsync(System::Net::IPEndPoint ^ remoteEP, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask ConnectAsync (System.Net.IPEndPoint remoteEP, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : System.Net.IPEndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Function ConnectAsync (remoteEP As IPEndPoint, cancellationToken As CancellationToken) As ValueTask

Parameters

remoteEP
IPEndPoint

The IPEndPoint to which you intend to connect.

cancellationToken
CancellationToken

A cancellation token used to propagate notification that this operation should be canceled.

Returns

A task representing the asynchronous operation.

Exceptions

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

Remarks

This operation will not block. The returned Returns Task object will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified IPEndPoint. Before you call Connect, you must create an instance of the IPEndPoint class using an IP address and a port number. Use this IPEndPoint as the remoteEP parameter. The Connect method will block until it either connects or fails. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive NotSupportedException with message This protocol version is not supported while using IPv6 address, then make sure you enabled IPv6 in constructor by passing InterNetworkV6.

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(IPEndPoint).

Applies to

ConnectAsync(IPAddress[], Int32)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ ConnectAsync(cli::array <System::Net::IPAddress ^> ^ addresses, int port);
public System.Threading.Tasks.Task ConnectAsync (System.Net.IPAddress[] addresses, int port);
member this.ConnectAsync : System.Net.IPAddress[] * int -> System.Threading.Tasks.Task
Public Function ConnectAsync (addresses As IPAddress(), port As Integer) As Task

Parameters

addresses
IPAddress[]

The IPAddress array of the host to which you intend to connect.

port
Int32

The port number to which you intend to connect.

Returns

The task object representing the asynchronous operation.

Exceptions

The ipAddresses parameter is null.

The port number is not valid.

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.

This method is valid for sockets that use the InterNetwork flag or the InterNetworkV6 flag.

Remarks

This operation will not block. The returned Returns Task object will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. Call this method to establish a synchronous remote host connection to the host specified by the array of IPAddress elements and the port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive NotSupportedException with message This protocol version is not supported while using IPv6 address, then make sure you enabled IPv6 in constructor by passing InterNetworkV6.

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).

See also

Applies to

ConnectAsync(IPAddress, Int32)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ ConnectAsync(System::Net::IPAddress ^ address, int port);
public System.Threading.Tasks.Task ConnectAsync (System.Net.IPAddress address, int port);
member this.ConnectAsync : System.Net.IPAddress * int -> System.Threading.Tasks.Task
Public Function ConnectAsync (address As IPAddress, port As Integer) As Task

Parameters

address
IPAddress

The IPAddress of the host to which you intend to connect.

port
Int32

The port number to which you intend to connect.

Returns

The task object representing the asynchronous operation.

Exceptions

The address parameter is null.

The port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Remarks

This operation will not block. The returned Returns Task object will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified IPAddress and port number as an asynchronous operation. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive NotSupportedException with message This protocol version is not supported while using IPv6 address, then make sure you enabled IPv6 in constructor by passing InterNetworkV6.

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).

See also

Applies to

ConnectAsync(IPEndPoint)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ ConnectAsync(System::Net::IPEndPoint ^ remoteEP);
public System.Threading.Tasks.Task ConnectAsync (System.Net.IPEndPoint remoteEP);
member this.ConnectAsync : System.Net.IPEndPoint -> System.Threading.Tasks.Task
Public Function ConnectAsync (remoteEP As IPEndPoint) As Task

Parameters

remoteEP
IPEndPoint

The IPEndPoint to which you intend to connect.

Returns

A task representing the asynchronous operation.

Remarks

This operation will not block. The returned Task object will complete after the TCP connection has been established. This method does not block the calling thread while the connection request is underway.

Call this method to establish a synchronous remote host connection to the specified IPEndPoint. Before you call Connect, you must create an instance of the IPEndPoint class using an IP address and a port number. Use this IPEndPoint as the remoteEP parameter. The Connect method will block until it either connects or fails. After connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. Use this NetworkStream to send and receive data.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you've obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Note

If you receive a NotSupportedException with the message "This protocol version is not supported" while using IPv6 address, make sure you enabled IPv6 in the constructor by passing InterNetworkV6.

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(IPEndPoint).

Applies to