TcpClient クラス
この記事の内容
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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
、同期ブロッキング モードでネットワーク経由でストリーム データを接続、送信、受信するための簡単なメソッドが用意されています。
データを接続して交換するにはTcpClient
、TcpListenerTCP ProtocolType で作成された または Socket が受信接続要求をリッスンしている必要があります。 このリスナーには、次の 2 つの方法のいずれかで接続できます。
をCreate
TcpClient
し、使用可能な Connect 3 つのメソッドのいずれかを呼び出します。リモート ホストの
TcpClient
ホスト名とポート番号を使用して をCreateします。 このコンストラクターは自動的に接続を試行します。
注意
コネクションレス データグラムを同期ブロッキング モードで送信する場合は、 クラスを使用します UdpClient 。
データを送受信するには、 メソッドを GetStream() 使用して を取得します NetworkStream。 Write(Byte[], Int32, Int32)の メソッドと Read(Byte[], Int32, Int32) メソッドをNetworkStream呼び出して、リモート ホストでデータを送受信します。 に Close(Int32) 関連付けられているすべてのリソースを解放するには、 メソッドを使用します TcpClient。
Tcp |
TcpClient クラスの新しいインスタンスを初期化します。 |
Tcp |
ファミリを指定して、TcpClient クラスの新しいインスタンスを初期化します。 |
Tcp |
TcpClient クラスの新しいインスタンスを初期化し、指定したローカル エンドポイントにバインドします。 |
Tcp |
TcpClient クラスの新しいインスタンスを初期化し、指定したホストの指定したポートに接続します。 |
Active |
接続されたかどうかを示す値を取得または設定します。 |
Available |
ネットワークから受信した、読み取り可能なデータ量を取得します。 |
Client |
基になる Socket を取得または設定します。 |
Connected | |
Exclusive |
TcpClient で 1 つのクライアントだけがポートを使用できるかどうかを指定する Boolean 値を取得または設定します。 |
Linger |
関連付けられているソケットの待機状態に関する情報を取得または設定します。 |
No |
送信バッファーまたは受信バッファーが設定されているサイズを超えていない場合に、遅延を無効にする値を取得または設定します。 |
Receive |
受信バッファーのサイズを取得または設定します。 |
Receive |
読み取り操作が開始された後に TcpClient がデータの受信を待機する時間を取得または設定します。 |
Send |
送信バッファーのサイズを取得または設定します。 |
Send |
送信操作が正常に完了するのを TcpClient が待機する時間を取得または設定します。 |
Begin |
リモート ホスト接続への非同期要求を開始します。 リモート ホストは、IPAddress とポート番号 (Int32) で指定されます。 |
Begin |
リモート ホスト接続への非同期要求を開始します。 リモート ホストは、IPAddress 配列とポート番号 (Int32) で指定されます。 |
Begin |
リモート ホスト接続への非同期要求を開始します。 リモート ホストは、ホスト名 (String) とポート番号 (Int32) で指定されます。 |
Close() |
この TcpClient インスタンスを破棄し、基になる TCP 接続を終了するように要求します。 |
Connect(IPAddress, Int32) |
指定された IP アドレスとポート番号を使用してクライアントをリモート TCP ホストに接続します。 |
Connect(IPAddress[], Int32) |
指定された IP アドレスとポート番号を使用してクライアントをリモート TCP ホストに接続します。 |
Connect(IPEnd |
指定されたリモート ネットワーク エンドポイントを使用してリモート TCP ホストにクライアントを接続します。 |
Connect(String, Int32) |
指定されたホストの指定されたポートにクライアントを接続します。 |
Connect |
指定された IP アドレスとポート番号を使用して、非同期操作としてクライアントをリモート TCP ホストに接続します。 |
Connect |
指定された IP アドレスとポート番号を使用して、非同期操作としてクライアントをリモート TCP ホストに接続します。 |
Connect |
指定された IP アドレスとポート番号を使用して、非同期操作としてクライアントをリモート TCP ホストに接続します。 |
Connect |
指定された IP アドレスとポート番号を使用して、非同期操作としてクライアントをリモート TCP ホストに接続します。 |
Connect |
指定したエンドポイントを非同期操作として使用して、クライアントをリモート TCP ホストに接続します。 |
Connect |
指定したエンドポイントを非同期操作として使用して、クライアントをリモート TCP ホストに接続します。 |
Connect |
非同期操作として、クライアントを、指定したホストの指定した TCP ポートに接続します。 |
Connect |
非同期操作として、クライアントを、指定したホストの指定した TCP ポートに接続します。 |
Dispose() |
TcpClient によって使用されているマネージド リソースおよびアンマネージド リソースを解放します。 |
Dispose(Boolean) |
TcpClient によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。 |
End |
保留中の非同期接続の試行を終了します。 |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
Finalize() |
TcpClient クラスによって使用されていたリソースを解放します。 |
Get |
既定のハッシュ関数として機能します。 (継承元 Object) |
Get |
データの送受信に使用する NetworkStream を返します。 |
Get |
現在のインスタンスの Type を取得します。 (継承元 Object) |
Memberwise |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
To |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
IDisposable. |
この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。 TcpClient によって使用されているすべてのリソースを解放します。 |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。