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

TcpClient クラス

TCP ネットワーク サービスのためのクライアント接続を提供します。

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

Public Class TcpClient
    Implements IDisposable
public class TcpClient implements IDisposable
public class TcpClient implements IDisposable
解説解説
使用例使用例

TcpClient 接続を確立するコード例を次に示します。

Shared Sub Connect(server As
 [String], message As [String])
   Try
      ' Create a TcpClient.
      ' Note, for this client to work you need to have a TcpServer 
      ' connected to the same address as specified by the server, port
      ' combination.
      Dim port As Int32 = 13000
      Dim client As New
 TcpClient(server, port)
      
      ' Translate the passed message into ASCII and store it as a Byte
 array.
      Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
      
      ' Get a client stream for reading and writing.
      '  Stream stream = client.GetStream();
      Dim stream As NetworkStream = client.GetStream()
      
      ' Send the message to the connected TcpServer. 
      stream.Write(data, 0, data.Length)
      
      Console.WriteLine("Sent: {0}", message)
      
      ' Receive the TcpServer.response.
      ' Buffer to store the response bytes.
      data = New [Byte](256) {}
      
      ' String to store the response ASCII representation.
      Dim responseData As [String] = [String].Empty
      
      ' Read the first batch of the TcpServer response bytes.
      Dim bytes As Int32 = stream.Read(data,
 0, data.Length)
      responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
      Console.WriteLine("Received: {0}", responseData)
      
      ' Close everything.
      stream.Close()
      client.Close()
   Catch e As ArgumentNullException
      Console.WriteLine("ArgumentNullException: {0}",
 e)
   Catch e As SocketException
      Console.WriteLine("SocketException: {0}", e)
   End Try
   
   Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
   Console.Read()
End Sub 'Connect
static void Connect(String server, String message)
 
{
  try 
  {
    // Create a TcpClient.
    // Note, for this client to work you need to have a TcpServer 
    // connected to the same address as specified by the server, port
    // combination.
    Int32 port = 13000;
    TcpClient client = new TcpClient(server, port);
    
    // Translate the passed message into ASCII and store it as a Byte
 array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

    // Get a client stream for reading and writing.
   //  Stream stream = client.GetStream();
    
    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);

    Console.WriteLine("Sent: {0}", message);         

    // Receive the TcpServer.response.
    
    // Buffer to store the response bytes.
    data = new Byte[256];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    Console.WriteLine("Received: {0}", responseData);         

    // Close everything.
    stream.Close();         
    client.Close();         
  } 
  catch (ArgumentNullException e) 
  {
    Console.WriteLine("ArgumentNullException: {0}", e);
  } 
  catch (SocketException e) 
  {
    Console.WriteLine("SocketException: {0}", e);
  }
    
  Console.WriteLine("\n Press Enter to continue...");
  Console.Read();
}
void Connect( String^ server, String^ message )
{
   try
   {
      // Create a TcpClient.
      // Note, for this client to work you need to have a TcpServer
 
      // connected to the same address as specified by the server, port
      // combination.
      Int32 port = 13000;
      TcpClient^ client = gcnew TcpClient( server,port );
      
      // Translate the passed message into ASCII and store it as a Byte
 array.
      array<Byte>^data = Text::Encoding::ASCII->GetBytes( message );
      
      // Get a client stream for reading and writing.
      //  Stream stream = client->GetStream();

      NetworkStream^ stream = client->GetStream();
      
      // Send the message to the connected TcpServer. 
      stream->Write( data, 0, data->Length );

      Console::WriteLine( "Sent: {0}", message );
      
      // Receive the TcpServer::response.

      // Buffer to store the response bytes.
      data = gcnew array<Byte>(256);

      // String to store the response ASCII representation.
      String^ responseData = String::Empty;
      
      // Read the first batch of the TcpServer response bytes.
      Int32 bytes = stream->Read( data, 0, data->Length );
      responseData = Text::Encoding::ASCII->GetString( data, 0, bytes );
      Console::WriteLine( "Received: {0}", responseData );
      
      // Close everything.
      client->Close();
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e );
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine( "SocketException: {0}", e );
   }

   Console::WriteLine( "\n Press Enter to continue..." );
   Console::Read();
}
static void Connect(String server, String message)
{
    try {
        // Create a TcpClient.
        // Note, for this client to work you need to have a TcpServer
 
        // connected to the same address as specified by the server,
 port
        // combination.
        Int32 port = (Int32)13000;
        TcpClient client = new TcpClient(server, (int)port);

        // Translate the passed message into ASCII and store
        // it as a Byte array.
        System.Byte data[] = (System.Byte[])
            System.Text.Encoding.get_ASCII().GetBytes(message);

        // Get a client stream for reading and writing.
        //  Stream stream = client.GetStream();
        NetworkStream stream = client.GetStream();

        // Send the message to the connected TcpServer. 
        stream.Write((ubyte[])data, 0, data.length);
        Console.WriteLine("Sent: {0}", message);

        // Receive the TcpServer.response.
        // Buffer to store the response bytes.
        data = new System.Byte[256];

        // String to store the response ASCII representation.
        String responseData = "";

        // Read the first batch of the TcpServer response bytes.
        Int32 bytes = (Int32)stream.Read((ubyte[])data, 0, data.length);
        responseData = System.Text.Encoding.get_ASCII().GetString(
            (ubyte[])data, 0, (int)bytes);
        Console.WriteLine("Received: {0}", responseData);

        // Close everything.
        client.Close();
    }
    catch (ArgumentNullException e) {
        Console.WriteLine("ArgumentNullException: {0}", e);
    }
    catch (SocketException e) {
        Console.WriteLine("SocketException: {0}", e);
    }
    Console.WriteLine("\n Press Enter to continue...");
    Console.Read();
} //Connect
.NET Framework のセキュリティ.NET Framework のセキュリティ
継承階層継承階層
System.Object
  System.Net.Sockets.TcpClient
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient メンバ
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙体
IPEndPoint クラス
Connect
Write
Read
その他の技術情報
TCP/UDP

TcpClient コンストラクタ ()

TcpClient クラスの新しいインスタンスを初期化します。

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

解説解説
使用例使用例

既定のコンストラクタを使用して新しい TcpClient を作成する方法を次のコード例に示します。

'Creates a TCPClient using the default constructor.
Dim tcpClientC As New TcpClient
//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = new TcpClient ();
//Creates a TCPClient using the default constructor.
TcpClient^ tcpClientC = gcnew TcpClient;

//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = new TcpClient();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
Connect

TcpClient コンストラクタ (String, Int32)

TcpClient クラスの新しいインスタンスを初期化し、指定したホストの指定したポートに接続します。

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

Public Sub New ( _
    hostname As String, _
    port As Integer _
)
Dim hostname As String
Dim port As Integer

Dim instance As New TcpClient(hostname,
 port)
public TcpClient (
    string hostname,
    int port
)
public:
TcpClient (
    String^ hostname, 
    int port
)
public TcpClient (
    String hostname, 
    int port
)
public function TcpClient (
    hostname : String, 
    port : int
)

パラメータ

hostname

接続先のリモート ホストの DNS 名。

port

接続先のリモート ホストのポート番号。

例外例外
例外の種類条件

ArgumentNullException

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

ArgumentOutOfRangeException

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

SocketException

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

解説解説
使用例使用例

ホスト名とポート番号を使用して TcpClient クラスのインスタンスを作成する方法を次のコード例に示します。

'Creates a TCPClient using hostname and port.

Dim tcpClientB As New TcpClient("www.contoso.com",
 11000)

//Creates a TCPClient using host name and port.
TcpClient tcpClientB = new TcpClient ("www.contoso.com",
 11000);
// Creates a TCPClient using hostname and port.
TcpClient^ tcpClientB = gcnew TcpClient( "www.contoso.com",11000 );

//Creates a TCPClient using host name and port.
TcpClient tcpClientB = new TcpClient("www.contoso.com",
 11000);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
TcpClient
Connect

TcpClient コンストラクタ (IPEndPoint)

TcpClient クラスの新しいインスタンスを初期化し、指定したローカル エンドポイントにバインドします。

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

Public Sub New ( _
    localEP As IPEndPoint _
)
Dim localEP As IPEndPoint

Dim instance As New TcpClient(localEP)
public TcpClient (
    IPEndPoint localEP
)
public:
TcpClient (
    IPEndPoint^ localEP
)
public TcpClient (
    IPEndPoint localEP
)
public function TcpClient (
    localEP : IPEndPoint
)

パラメータ

localEP

TCP Socket のバインド先の IPEndPoint。

例外例外
例外の種類条件

ArgumentNullException

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

解説解説
使用例使用例

ローカル エンドポイントを使用して TcpClient クラスのインスタンスを作成する方法を次のコード例に示します。

'Creates a TCPClient using a local endpoint.
Dim ipAddress As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(0)
   Dim ipLocalEndPoint As New
 IPEndPoint(ipAddress, 0)

   Dim tcpClientA As New
 TcpClient(ipLocalEndPoint)

//Creates a TCPClient using a local end point.
IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
TcpClient tcpClientA = new TcpClient (ipLocalEndPoint);
//Creates a TCPClient using a local end point.
IPAddress^ ipAddress = Dns::Resolve( Dns::GetHostName() )->AddressList[ 0 ];
IPEndPoint^ ipLocalEndPoint = gcnew IPEndPoint( ipAddress,11000 );
TcpClient^ tcpClientA = gcnew TcpClient( ipLocalEndPoint );

//Creates a TCPClient using a local end point.
IPAddress ipAddress =
    Dns.Resolve(Dns.GetHostName()).get_AddressList()[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
TcpClient tcpClientA = new TcpClient(ipLocalEndPoint);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
IPEndPoint クラス
Connect

TcpClient コンストラクタ (AddressFamily)

ファミリを指定して、TcpClient クラスの新しいインスタンスを初期化します。

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

Public Sub New ( _
    family As AddressFamily _
)
Dim family As AddressFamily

Dim instance As New TcpClient(family)
public TcpClient (
    AddressFamily family
)
public:
TcpClient (
    AddressFamily family
)
public TcpClient (
    AddressFamily family
)
public function TcpClient (
    family : AddressFamily
)

パラメータ

family

IP プロトコルの AddressFamily。

例外例外
例外の種類条件

ArgumentException

family != AddressFamily.InterNetwork

または

family != AddressFamily.InterNetworkV6

解説解説
使用例使用例

TcpClient クラスのインスタンスを作成する方法を次のコード例に示します。

Dim tcpClientD As New TcpClient(AddressFamily.InterNetwork)
TcpClient tcpClientD = new TcpClient (AddressFamily.InterNetwork);
TcpClient^ tcpClientD = gcnew TcpClient( AddressFamily::InterNetwork );

TcpClient tcpClientD =
    new TcpClient(AddressFamily.InterNetwork);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間

TcpClient コンストラクタ


TcpClient プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Active 接続されたかどうかを示す値を取得または設定します。
参照参照

関連項目

TcpClient クラス
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙体
IPEndPoint クラス
Connect
Write
Read

その他の技術情報

TCP/UDP

TcpClient メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose TcpClient によって使用されているすべてのリソースを解放します。
参照参照

関連項目

TcpClient クラス
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙体
IPEndPoint クラス
Connect
Write
Read

その他の技術情報

TCP/UDP

TcpClient メンバ

TCP ネットワーク サービスのためのクライアント接続を提供します。

TcpClient データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Active 接続されたかどうかを示す値を取得または設定します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose TcpClient によって使用されているすべてのリソースを解放します。
参照参照

関連項目

TcpClient クラス
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙体
IPEndPoint クラス
Connect
Write
Read

その他の技術情報

TCP/UDP



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「TcpClient」の関連用語

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

   

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



TcpClientのページの著作権

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

©2026 GRAS Group, Inc.RSS