TcpClient クラス

定義

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

public ref class TcpClient : IDisposable
public class TcpClient : IDisposable
type TcpClient = class
    interface IDisposable
Public Class TcpClient
Implements IDisposable
継承
TcpClient
実装

次のコード例では、 TcpClient 接続を確立します。

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;

    // Prefer a using declaration to ensure the instance is Disposed later.
    using 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.
    NetworkStream stream = client.GetStream();

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

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

    // Receive the server 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);

    // Explicit close is not necessary since TcpClient.Dispose() will be
    // called automatically.
    // 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();
}
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

      ' Prefer using declaration to ensure the instance is Disposed later.
      Using 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.
         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 server 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)
         
         ' Explicit close is not necessary since TcpClient.Dispose() will be
         ' called automatically.
         ' stream.Close()
         ' client.Close()
      End Using
   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

注釈

TcpClient クラスは、同期ブロッキング モードでネットワーク経由でストリーム データを接続、送信、および受信するための簡単なメソッドを提供します。

TcpClientが接続してデータを交換するには、TCP ProtocolTypeで作成されたTcpListenerまたはSocketが受信接続要求をリッスンしている必要があります。 このリスナーには、次の 2 つの方法のいずれかで接続できます。

  • TcpClientを作成し、使用可能な 3 つのConnect メソッドのいずれかを呼び出します。

  • リモート ホストのホスト名とポート番号を使用して、 TcpClient を作成します。 このコンストラクターは自動的に接続を試行します。

Note

コネクションレス データグラムを同期ブロッキング モードで送信する場合は、 UdpClient クラスを使用します。

注意 (継承者)

データを送受信するには、 GetStream() メソッドを使用して NetworkStreamを取得します。 リモート ホストでデータを送受信するには、NetworkStreamWrite(Byte[], Int32, Int32)メソッドとRead(Byte[], Int32, Int32) メソッドを呼び出します。 Close(Int32) メソッドを使用して、TcpClientに関連付けられているすべてのリソースを解放します。

コンストラクター

名前 説明
TcpClient()

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

TcpClient(AddressFamily)

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

TcpClient(IPEndPoint)

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

TcpClient(String, Int32)

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

プロパティ

名前 説明
Active

接続が確立されたかどうかを示す値を取得または設定します。

Available

ネットワークから受信され、読み取ることができるデータの量を取得します。

Client

基になる Socketを取得または設定します。

Connected

TcpClientの基になるSocketがリモート ホストに接続されているかどうかを示す値を取得します。

ExclusiveAddressUse

Booleanが 1 つのクライアントのみにポートの使用を許可するかどうかを指定するTcpClient値を取得または設定します。

LingerState

関連付けられているソケットの残留状態に関する情報を取得または設定します。

NoDelay

送受信バッファーがいっぱいでない場合に遅延を無効にする値を取得または設定します。

ReceiveBufferSize

受信バッファーのサイズを取得または設定します。

ReceiveTimeout

読み取り操作が開始されると、 TcpClient がデータの受信を待機する時間を取得または設定します。

SendBufferSize

送信バッファーのサイズを取得または設定します。

SendTimeout

送信操作が正常に完了するまで TcpClient が待機する時間を取得または設定します。

メソッド

名前 説明
BeginConnect(IPAddress, Int32, AsyncCallback, Object)

リモート ホスト接続の非同期要求を開始します。 リモート ホストは、 IPAddress とポート番号 (Int32) で指定されます。

BeginConnect(IPAddress[], Int32, AsyncCallback, Object)

リモート ホスト接続の非同期要求を開始します。 リモート ホストは、 IPAddress 配列とポート番号 (Int32) で指定されます。

BeginConnect(String, Int32, AsyncCallback, Object)

リモート ホスト接続の非同期要求を開始します。 リモート ホストは、ホスト名 (String) とポート番号 (Int32) で指定されます。

Close()

この TcpClient インスタンスを破棄し、基になる TCP 接続を閉じる要求を行います。

Connect(IPAddress, Int32)

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

Connect(IPAddress[], Int32)

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

Connect(IPEndPoint)

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

Connect(String, Int32)

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

ConnectAsync(IPAddress, Int32)

指定した IP アドレスとポート番号を非同期操作として使用して、クライアントをリモート TCP ホストに接続します。

ConnectAsync(IPAddress[], Int32)

指定した IP アドレスとポート番号を非同期操作として使用して、クライアントをリモート TCP ホストに接続します。

ConnectAsync(String, Int32)

非同期操作として、指定したホスト上の指定された TCP ポートにクライアントを接続します。

Dispose()

TcpClientによって使用されるマネージド リソースとアンマネージド リソースを解放します。

Dispose(Boolean)

TcpClientによって使用されるアンマネージ リソースを解放し、必要に応じてマネージド リソースを解放します。

EndConnect(IAsyncResult)

保留中の非同期接続試行を終了します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Finalize()

TcpClient クラスによって使用されるリソースを解放します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetStream()

データの送受信に使用する NetworkStream を返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
IDisposable.Dispose()

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

TcpClientによって使用されるすべてのリソースを解放します。

適用対象

こちらもご覧ください