TcpClient クラス

定義

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

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

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

void Connect( String^ server, String^ message )
{
   TcpClient^ client = nullptr;
   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;
      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.
      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 = 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 );
      
      // Explicit close is not necessary since TcpClient::Dispose() will be
      // called automatically in finally block.
      // stream->Close();
      // client->Close();
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e );
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine( "SocketException: {0}", e );
   }
   finally
   {
      if (client != nullptr)
         delete client;
   }

   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 = 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 、同期ブロッキング モードでネットワーク経由でストリーム データを接続、送信、受信するための簡単なメソッドが用意されています。

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

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

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

Note

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

注意 (継承者)

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

コンストラクター

TcpClient()

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

TcpClient(AddressFamily)

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

TcpClient(IPEndPoint)

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

TcpClient(String, Int32)

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

プロパティ

Active

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

Available

ネットワークから受信した、読み取り可能なデータ量を取得します。

Client

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

Connected

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

ExclusiveAddressUse

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

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, CancellationToken)

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

ConnectAsync(IPAddress[], Int32)

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

ConnectAsync(IPAddress[], Int32, CancellationToken)

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

ConnectAsync(IPEndPoint)

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

ConnectAsync(IPEndPoint, CancellationToken)

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

ConnectAsync(String, Int32)

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

ConnectAsync(String, Int32, CancellationToken)

非同期操作として、クライアントを、指定したホストの指定した 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 によって使用されているすべてのリソースを解放します。

適用対象

こちらもご覧ください