TcpClient.Connect 메서드

정의

지정된 호스트 이름과 포트 번호를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

오버로드

Connect(IPEndPoint)

지정된 네트워크 엔드포인트를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

Connect(IPAddress, Int32)

지정된 IP 주소와 포트 번호를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

Connect(IPAddress[], Int32)

지정된 IP 주소와 포트 번호를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

Connect(String, Int32)

지정된 호스트의 지정된 포트에 클라이언트를 연결합니다.

Connect(IPEndPoint)

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

지정된 네트워크 엔드포인트를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

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)

매개 변수

remoteEP
IPEndPoint

연결할 IPEndPoint입니다.

예외

remoteEp 매개 변수가 null인 경우

소켓에 액세스할 때 오류가 발생했습니다.

TcpClient가 닫혔습니다.

예제

다음 코드 예제에서는 를 사용하여 IPEndPoint 원격 호스트와 연결합니다.

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

설명

이 메서드를 호출하여 지정된 IPEndPoint에 대한 동기 원격 호스트 연결을 설정합니다. 를 호출Connect하기 전에 IP 주소와 포트 번호를 사용하여 클래스의 IPEndPoint instance 만들어야 합니다. 이를 IPEndPoint 매개 변수로 remoteEP 사용합니다. 메서드는 Connect 연결되거나 실패할 때까지 차단됩니다. 원격 호스트와 연결한 후 메서드를 GetStream 사용하여 기본 NetworkStream를 가져옵니다. 이를 NetworkStream 사용하여 데이터를 보내고 받습니다.

참고

가 수신되면 를 SocketException사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후에는 Windows 소켓 버전 2 API 오류 코드 설명서에서 오류에 대한 자세한 설명을 참조할 수 있습니다.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

참고

IPv6 주소를 사용하는 동안 메시지 This protocol version is not supported 와 함께 NotSupportedException을 수신하는 경우 를 전달 InterNetworkV6하여 생성자에서 IPv6을 사용하도록 설정했는지 확인합니다.

추가 정보

적용 대상

Connect(IPAddress, Int32)

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

지정된 IP 주소와 포트 번호를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

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)

매개 변수

address
IPAddress

연결할 호스트의 IPAddress입니다.

port
Int32

연결할 포트 번호입니다.

예외

address 매개 변수가 null인 경우

portMinPortMaxPort 사이에 없는 경우

소켓에 액세스할 때 오류가 발생했습니다.

TcpClient가 닫혀 있습니다.

예제

다음 코드 예제에서는 IP 주소 및 포트 번호를 사용하여 원격 호스트와 연결합니다.

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

설명

지정된 IPAddress 및 포트 번호에 대한 동기 원격 호스트 연결을 설정하려면 이 메서드를 호출합니다. 메서드는 Connect 연결되거나 실패할 때까지 차단됩니다. 원격 호스트와 연결한 후 메서드를 GetStream 사용하여 기본 NetworkStream를 가져옵니다. 이를 NetworkStream 사용하여 데이터를 보내고 받습니다.

참고

가 수신되면 를 SocketException사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후에는 Windows 소켓 버전 2 API 오류 코드 설명서에서 오류에 대한 자세한 설명을 참조할 수 있습니다.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

참고

IPv6 주소를 사용하는 동안 메시지 This protocol version is not supported 와 함께 NotSupportedException을 수신하는 경우 를 전달 InterNetworkV6하여 생성자에서 IPv6을 사용하도록 설정했는지 확인합니다.

추가 정보

적용 대상

Connect(IPAddress[], Int32)

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

지정된 IP 주소와 포트 번호를 사용하여 원격 TCP 호스트에 클라이언트를 연결합니다.

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)

매개 변수

ipAddresses
IPAddress[]

연결할 호스트의 IPAddress 배열입니다.

port
Int32

연결할 포트 번호입니다.

예외

ipAddresses 매개 변수가 null인 경우

포트 번호가 잘못된 경우

소켓에 액세스하는 동안 오류가 발생했습니다.

Socket이 닫혔습니다.

호출 스택의 상위 호출자에게 요청된 작업을 수행할 권한이 없는 경우

이 메서드는 InterNetwork 플래그 또는 InterNetworkV6 플래그를 사용하는 소켓에 대해서만 사용할 수 있습니다.

예제

다음 코드 예제에서는 IP 주소 및 포트 번호를 사용하여 원격 호스트와 연결합니다.

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");
}

설명

이 메서드는 일반적으로 단일 호스트에 대해 여러 IP 주소를 반환할 BeginGetHostAddresses 수 있는 메서드를 호출한 직후에 사용됩니다. 메서드를 Connect 호출하여 요소 배열 IPAddress 및 포트 번호로 지정된 호스트에 대한 동기 원격 호스트 연결을 설정합니다. 메서드는 Connect 연결되거나 실패할 때까지 차단됩니다. 원격 호스트와 연결한 후 메서드를 GetStream 사용하여 기본 NetworkStream를 가져옵니다. 이를 NetworkStream 사용하여 데이터를 보내고 받습니다.

참고

가 수신되면 를 SocketException사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후에는 Windows 소켓 버전 2 API 오류 코드 설명서에서 오류에 대한 자세한 설명을 참조할 수 있습니다.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

참고

IPv6 주소를 사용하는 동안 메시지 This protocol version is not supported 와 함께 NotSupportedException을 수신하는 경우 를 전달 InterNetworkV6하여 생성자에서 IPv6을 사용하도록 설정했는지 확인합니다.

추가 정보

적용 대상

Connect(String, Int32)

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

지정된 호스트의 지정된 포트에 클라이언트를 연결합니다.

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)

매개 변수

hostname
String

연결할 원격 호스트의 DNS 이름입니다.

port
Int32

연결한 원격 호스트의 포트 번호입니다.

예외

hostname 매개 변수가 null인 경우

port 매개 변수가 MinPortMaxPort 사이에 없습니다.

소켓에 액세스할 때 오류가 발생했습니다.

TcpClient가 닫혀 있습니다.

예제

다음 코드 예제에서는 호스트 이름 및 포트 번호를 사용하여 원격 호스트와 연결합니다.

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

설명

이 메서드를 호출하여 지정된 호스트 이름 및 포트 번호에 대한 동기 원격 호스트 연결을 설정합니다. 메서드는 Connect 연결되거나 실패할 때까지 차단됩니다. 원격 호스트와 연결한 후 메서드를 GetStream 사용하여 기본 NetworkStream를 가져옵니다. 이를 NetworkStream 사용하여 데이터를 보내고 받습니다.

IPv6을 사용하도록 설정하고 Connect(String, Int32) 메서드를 호출하여 IPv6 및 IPv4 주소로 확인되는 호스트에 연결하는 경우 IPv4 주소 전에 먼저 IPv6 주소에 대한 연결이 시도됩니다. 호스트가 IPv6 주소에서 수신 대기하지 않는 경우 연결 설정 시간을 지연시키는 효과가 있을 수 있습니다.

참고

가 수신되면 를 SocketException사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후에는 Windows 소켓 버전 2 API 오류 코드 설명서에서 오류에 대한 자세한 설명을 참조할 수 있습니다.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

추가 정보

적용 대상