TcpClient.ConnectAsync 方法

定义

使用指定的主机名和端口号将客户端连接到远程 TCP 主机以作为异步操作。Connects the client to a remote TCP host using the specified host name and port number as an asynchronous operation.

重载

ConnectAsync(IPAddress[], Int32, CancellationToken)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

ConnectAsync(String, Int32, CancellationToken)

将客户端连接到指定主机上的指定 TCP 端口以作为异步操作。Connects the client to the specified TCP port on the specified host as an asynchronous operation.

ConnectAsync(IPAddress, Int32, CancellationToken)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

ConnectAsync(String, Int32)

将客户端连接到指定主机上的指定 TCP 端口以作为异步操作。Connects the client to the specified TCP port on the specified host as an asynchronous operation.

ConnectAsync(IPAddress[], Int32)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。Connects the client to a remote TCP host using the specified IP addresses and port number as an asynchronous operation.

ConnectAsync(IPAddress, Int32)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。Connects the client to a remote TCP host using the specified IP address and port number as an asynchronous operation.

ConnectAsync(IPAddress[], Int32, CancellationToken)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。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

参数

addresses
IPAddress[]

远程主机的 IP 地址数组。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.

返回

ValueTask

表示异步连接操作的任务。A task that represents the asynchronous connection operation.

例外

ipAddresses 参数为 nullThe ipAddresses parameter is null.

端口号无效。The port number is not valid.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

Socket 已关闭。The Socket has been closed.

调用堆栈中的较高调用方无权执行所请求的操作。A caller higher in the call stack does not have permission for the requested operation.

此方法对于使用 InterNetwork 标志或 InterNetworkV6 标志的套接字有效。This method is valid for sockets that use the InterNetwork flag or the InterNetworkV6 flag.

注解

此操作不会阻止。This operation will not block. 在建立 TCP 连接后,返回的任务将完成。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.

此方法通常在调用方法之后立即使用,此 BeginGetHostAddresses 方法可为单个主机返回多个 IP 地址。This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. 调用此方法以建立与 IP 地址数组指定的主机与异步操作的端口号的同步远程主机连接。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. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于

ConnectAsync(String, Int32, CancellationToken)

将客户端连接到指定主机上的指定 TCP 端口以作为异步操作。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

参数

host
String

远程主机的 DNS 名称。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.

返回

ValueTask

表示异步连接操作的任务。A task that represents the asynchronous connection operation.

例外

hostname 参数为 nullThe hostname parameter is null.

port 参数不介于 MinPortMaxPort 之间。The port parameter is not between MinPort and MaxPort.

访问套接字时出错。An error occurred when accessing the socket.

注解

此操作不会阻止。This operation will not block. 在建立 TCP 连接后,返回的任务将完成。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. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

如果启用了 IPv6,并且 ConnectAsync(String, Int32) 调用了方法来连接到解析 IPv6 和 ipv4 地址的主机,则在 IPv4 地址之前将首先尝试连接到 ipv6 地址。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. 如果主机未侦听 IPv6 地址,则这可能会延迟建立连接的时间。This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于

ConnectAsync(IPAddress, Int32, CancellationToken)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。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

参数

address
IPAddress

远程主机的 IP 地址。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.

返回

ValueTask

表示异步连接操作的任务。A task that represents the asynchronous connection operation.

例外

address 参数为 nullThe address parameter is null.

port 不在 MinPortMaxPort 之间。The port is not between MinPort and MaxPort.

访问套接字时出错。An error occurred when accessing the socket.

注解

此操作不会阻止。This operation will not block. 在建立 TCP 连接后,返回的任务将完成。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.

调用此方法以建立与指定 IP 地址和端口号的同步远程主机连接,作为异步操作。Call this method to establish a synchronous remote host connection to the specified IP address and port number as an asynchronous operation. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于

ConnectAsync(String, Int32)

将客户端连接到指定主机上的指定 TCP 端口以作为异步操作。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

参数

host
String

要连接到的远程主机的 DNS 名。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.

返回

Task

表示异步操作的任务对象。The task object representing the asynchronous operation.

例外

hostname 参数为 nullThe hostname parameter is null.

port 参数不介于 MinPortMaxPort 之间。The port parameter is not between MinPort and MaxPort.

访问套接字时出错。An error occurred when accessing the socket.

注解

此操作不会阻止。This operation will not block. Task 建立 TCP 连接后,返回的返回对象将完成。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. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

如果启用了 IPv6,并且 ConnectAsync(String, Int32) 调用了方法来连接到解析 IPv6 和 ipv4 地址的主机,则在 IPv4 地址之前将首先尝试连接到 ipv6 地址。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. 如果主机未侦听 IPv6 地址,则这可能会延迟建立连接的时间。This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于

ConnectAsync(IPAddress[], Int32)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。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

参数

addresses
IPAddress[]

要连接到的主机的 IPAddress 数组。The IPAddress array of the host to which you intend to connect.

port
Int32

要连接到的端口号。The port number to which you intend to connect.

返回

Task

表示异步操作的任务对象。The task object representing the asynchronous operation.

例外

ipAddresses 参数为 nullThe ipAddresses parameter is null.

端口号无效。The port number is not valid.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

Socket 已关闭。The Socket has been closed.

调用堆栈中的较高调用方无权执行所请求的操作。A caller higher in the call stack does not have permission for the requested operation.

此方法对于使用 InterNetwork 标志或 InterNetworkV6 标志的套接字有效。This method is valid for sockets that use the InterNetwork flag or the InterNetworkV6 flag.

注解

此操作不会阻止。This operation will not block. Task 建立 TCP 连接后,返回的返回对象将完成。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.

此方法通常在调用方法之后立即使用,此 BeginGetHostAddresses 方法可为单个主机返回多个 IP 地址。This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. 调用此方法可建立与由元素数组和端口号指定的主机的同步远程主机连接 IPAddress ,作为异步操作。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. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于

ConnectAsync(IPAddress, Int32)

使用指定的 IP 地址和端口号将客户端连接到远程 TCP 主机以作为异步操作。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

参数

address
IPAddress

你打算连接到的主机的 IPAddressThe IPAddress of the host to which you intend to connect.

port
Int32

要连接到的端口号。The port number to which you intend to connect.

返回

Task

表示异步操作的任务对象。The task object representing the asynchronous operation.

例外

address 参数为 nullThe address parameter is null.

port 不在 MinPortMaxPort 之间。The port is not between MinPort and MaxPort.

访问套接字时出错。An error occurred when accessing the socket.

注解

此操作不会阻止。This operation will not block. Task 建立 TCP 连接后,返回的返回对象将完成。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.

调用此方法以将同步远程主机连接与指定的 IPAddress 和端口号建立为异步操作。Call this method to establish a synchronous remote host connection to the specified IPAddress and port number as an asynchronous operation. 与远程主机连接后,使用 GetStream 方法获取基础 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此 NetworkStream 来发送和接收数据。Use this NetworkStream to send and receive data.

备注

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于