Επεξεργασία

Κοινή χρήση μέσω


TcpClient.Connect Method

Definition

Connects the client to a remote TCP host using the specified host name and port number.

Overloads

Connect(IPEndPoint)

Connects the client to a remote TCP host using the specified remote network endpoint.

Connect(IPAddress, Int32)

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

Connect(IPAddress[], Int32)

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

Connect(String, Int32)

Connects the client to the specified port on the specified host.

Connect(IPEndPoint)

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

Connects the client to a remote TCP host using the specified remote network endpoint.

public:
 void Connect(System::Net::IPEndPoint ^ remoteEP);
public void Connect (System.Net.IPEndPoint remoteEP);
member this.Connect : System.Net.IPEndPoint -> unit
Public Sub Connect (remoteEP As IPEndPoint)

Parameters

remoteEP
IPEndPoint

The IPEndPoint to which you intend to connect.

Exceptions

The remoteEp parameter is null.

An error occurred when accessing the socket.

Examples

The following code example uses an IPEndPoint to connect with a remote host.

//Uses a remote end point to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddress,11004 );
tcpClient->Connect( ipEndPoint );
//Uses a remote endpoint to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, 11004);

tcpClient.Connect (ipEndPoint);
'Uses a remote endpoint to establish a socket connection.
Dim tcpClient As New TcpClient
Dim ipAddress As IPAddress = Dns.GetHostEntry("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)

tcpClient.Connect(ipEndPoint)

Remarks

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.

See also

Applies to

Connect(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.

public:
 void Connect(System::Net::IPAddress ^ address, int port);
public void Connect (System.Net.IPAddress address, int port);
member this.Connect : System.Net.IPAddress * int -> unit
Public Sub Connect (address As IPAddress, port As Integer)

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.

Exceptions

The address parameter is null.

The port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Examples

The following code example uses an IP Address and port number to connect with a remote host.

//Uses the IP address and port number to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
tcpClient->Connect( ipAddress, 11003 );
//Uses the IP address and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];

tcpClient.Connect (ipAddress, 11003);
'Uses the IP address and port number to establish a socket connection.
Dim tcpClient As New TcpClient
Dim ipAddress As IPAddress = Dns.GetHostEntry("www.contoso.com").AddressList(0)
tcpClient.Connect(ipAddress, 11003)

Remarks

Call this method to establish a synchronous remote host connection to the specified IPAddress and port number. 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.

See also

Applies to

Connect(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.

public:
 void Connect(cli::array <System::Net::IPAddress ^> ^ ipAddresses, int port);
public void Connect (System.Net.IPAddress[] ipAddresses, int port);
member this.Connect : System.Net.IPAddress[] * int -> unit
Public Sub Connect (ipAddresses As IPAddress(), port As Integer)

Parameters

ipAddresses
IPAddress[]

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

port
Int32

The port number to which you intend to connect.

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.

Examples

The following code example uses an IP Address and port number to connect with a remote host.

static void DoConnect( String^ host, int port )
{
   // Connect to the specified host.
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   array<IPAddress^>^IPAddresses = Dns::GetHostAddresses( host );
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->Connect( IPAddresses, port );
   Console::WriteLine( "Connection established" );
}
static void DoConnect(string host, int port)
{
    // Connect to the specified host.
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);

    IPAddress[] IPAddresses = Dns.GetHostAddresses(host);

    Console.WriteLine("Establishing connection to {0}", host);
    t.Connect(IPAddresses, port);

    Console.WriteLine("Connection established");
}

Remarks

This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. Call the Connect method to establish a synchronous remote host connection to the host specified by the array of IPAddress elements and the port number. 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.

See also

Applies to

Connect(String, Int32)

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

Connects the client to the specified port on the specified host.

public:
 void Connect(System::String ^ hostname, int port);
public void Connect (string hostname, int port);
member this.Connect : string * int -> unit
Public Sub Connect (hostname As String, port As Integer)

Parameters

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

Exceptions

The hostname parameter is null.

The port parameter is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Examples

The following code example uses the host name and port number to connect with a remote host.

//Uses a host name and port number to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
tcpClient->Connect( "www.contoso.com", 11002 );
//Uses a host name and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);
'Uses a host name and port number to establish a socket connection.
Dim tcpClient As New TcpClient()

   tcpClient.Connect("www.contoso.com", 11002)

Remarks

Call this method to establish a synchronous remote host connection to the specified host name and port number. 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.

If IPv6 is enabled and the Connect(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

Applies to