TcpClient.Connectとは? わかりやすく解説

TcpClient.Connect メソッド (String, Int32)

指定されたホストの指定されたポートにクライアントを接続します。

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

hostname が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

port が MinPort と MaxPort の間の値ではありません。

SocketException

ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。

ObjectDisposedException

TcpClient が閉じています。

解説解説
使用例使用例

ホスト名とポート番号を使用してリモート ホストと接続するコード例を次に示します。

'Uses a host name and port number to establish a socket connection.
Dim tcpClient As New 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.
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);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
Connect
GetStream

TcpClient.Connect メソッド (IPEndPoint)

指定されたリモート ネットワーク エンドポイントを使用してリモート TCP ホストにクライアントを接続します。

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Sub Connect ( _
    remoteEP As IPEndPoint _
)
Dim instance As TcpClient
Dim remoteEP As IPEndPoint

instance.Connect(remoteEP)
public void Connect (
    IPEndPoint remoteEP
)
public:
void Connect (
    IPEndPoint^ remoteEP
)
public void Connect (
    IPEndPoint remoteEP
)
public function Connect (
    remoteEP : IPEndPoint
)

パラメータ

remoteEP

接続先の IPEndPoint。

例外例外
例外の種類条件

ArgumentNullException

remoteEp が null 参照 (Visual Basic では Nothing) です。

SocketException

ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。

ObjectDisposedException

TcpClient が閉じています。

解説解説
使用例使用例

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)

//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 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 end point to establish a socket connection.
TcpClient tcpClient = new TcpClient();
IPAddress ipAddress =
    Dns.Resolve("www.contoso.com").get_AddressList()[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);

tcpClient.Connect(ipEndPoint);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
IPEndPoint クラス
GetStream

TcpClient.Connect メソッド (IPAddress, Int32)

指定された IP アドレスとポート番号を使用してクライアントをリモート TCP ホストと接続します。

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

address パラメータが null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

port が MinPort と MaxPort の間の値ではありません。

SocketException

ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。

ObjectDisposedException

TcpClient が閉じています。

解説解説
使用例使用例

IP アドレスとポート番号を使用してリモート ホストと接続するコード例を次に示します。

'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)
//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.
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.Resolve("www.contoso.com").get_AddressList()[0];

tcpClient.Connect(ipAddress, 11003);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
GetStream

TcpClient.Connect メソッド


TcpClient.Connect メソッド (IPAddress[], Int32)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定された IP アドレスとポート番号を使用してクライアントをリモート TCP ホストと接続します。

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Sub Connect ( _
    ipAddresses As IPAddress(), _
    port As Integer _
)
Dim instance As TcpClient
Dim ipAddresses As IPAddress()
Dim port As Integer

instance.Connect(ipAddresses, port)
public void Connect (
    IPAddress[] ipAddresses,
    int port
)
public:
void Connect (
    array<IPAddress^>^ ipAddresses, 
    int port
)
public void Connect (
    IPAddress[] ipAddresses, 
    int port
)

パラメータ

ipAddresses

接続先ホストの IPAddress 配列。

port

接続先のポート番号。

例外例外
例外の種類条件

ArgumentNullException

ipAddresses パラメータが null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

ポート番号が無効です。

SocketException

ソケットへのアクセスを試みているときにエラーが発生しました。詳細については「解説」を参照してください。

ObjectDisposedException

Socket は閉じられています。

SecurityException

コール スタックの上位にある呼び出し元が、要求された操作のアクセス許可を保持していません。

NotSupportedException

このメソッドは、InterNetwork フラグまたは InterNetworkV6 フラグを使用するソケットで有効です。

解説解説
使用例使用例

IP アドレスとポート番号を使用してリモート ホストと接続するコード例を次に示します。

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");
}
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" );
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
TcpListener



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「TcpClient.Connect」の関連用語

TcpClient.Connectのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TcpClient.Connectのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS