TcpClient.ConnectAsync TcpClient.ConnectAsync TcpClient.ConnectAsync 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) ConnectAsync(String, Int32) ConnectAsync(String, Int32) ConnectAsync(String, Int32)

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

ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32)

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

ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32)

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

ConnectAsync(String, Int32) ConnectAsync(String, Int32) ConnectAsync(String, Int32) ConnectAsync(String, Int32)

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 String String String

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

port
Int32 Int32 Int32 Int32

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

Returns

The task object representing the asynchronous operation.

Exceptions

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.

See also

ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32) ConnectAsync(IPAddress, Int32)

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 IPAddress IPAddress IPAddress

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

port
Int32 Int32 Int32 Int32

The port number to which you intend to connect.

Returns

The task object representing the asynchronous operation.

Exceptions

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.

See also

ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32) ConnectAsync(IPAddress[], Int32)

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 Int32 Int32 Int32

The port number to which you intend to connect.

Returns

The task object representing the asynchronous operation.

Exceptions

An error occurred when attempting to access the socket.

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

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.

See also

Applies to