TcpClient クラス
アセンブリ: System (system.dll 内)

Public Class TcpClient Implements IDisposable
public class TcpClient : IDisposable
public ref class TcpClient : IDisposable
public class TcpClient implements IDisposable
public class TcpClient implements IDisposable

TcpClient クラスは、同期ブロッキング モードのときにネットワーク上で接続したり、ストリーム データを送受信したりするための単純なメソッドを提供します。
TcpClient が接続およびデータ交換を行うには、TCP ProtocolType を使用して作成された TcpListener または Socket が受信接続要求を待機している必要があります。このリスナに接続するには、次の 2 つの方法のいずれかを使用します。
![]() |
---|

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


System.Net.Sockets.TcpClient


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TcpClient コンストラクタ ()
アセンブリ: System (system.dll 内)


このコンストラクタは、新しい TcpClient を作成し、基になるサービス プロバイダが最も適切なローカル IP アドレスおよびポート番号を割り当てられるようにします。データを送受信するには、まず、Connect メソッドを呼び出しておく必要があります。
![]() |
---|


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TcpClient コンストラクタ (String, Int32)
アセンブリ: System (system.dll 内)



このコンストラクタは、新しい TcpClient を作成し、指定したホスト名とポート番号への同期接続を試みます。基になるサービス プロバイダは、最も適切なローカル IP アドレスおよびポート番号を割り当てます。TcpClient は接続が成功するか失敗するまでブロックします。このコンストラクタにより初期化、DNS ホスト名の解決、接続を 1 つの便利な手順で実行できます。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。 |

ホスト名とポート番号を使用して 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 );

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TcpClient コンストラクタ (IPEndPoint)
アセンブリ: System (system.dll 内)



このコンストラクタは、新しい TcpClient を作成し、これを localEP パラメータで指定した IPEndPoint にバインドします。このコンストラクタを呼び出す前に、データの送受信を開始する IP アドレスおよびポート番号を使用して IPEndPoint を作成する必要があります。接続と通信を行う前にローカル IP アドレスおよびポート番号を指定する必要はありません。その他のコンストラクタを使用して TcpClient を作成する場合は、基になるサービス プロバイダが最も適切なローカル IP アドレスおよびポート番号を割り当てます。
このとき、データを送受信する前に、Connect メソッドを呼び出しておく必要があります。
![]() |
---|
このメンバは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。詳細については、「ネットワークのトレース」を参照してください。 |

ローカル エンドポイントを使用して 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);

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TcpClient コンストラクタ (AddressFamily)
アセンブリ: System (system.dll 内)




TcpClient クラスのインスタンスを作成する方法を次のコード例に示します。
TcpClient^ tcpClientD = gcnew TcpClient( AddressFamily::InterNetwork );

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TcpClient コンストラクタ

名前 | 説明 |
---|---|
TcpClient () | TcpClient クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
TcpClient (AddressFamily) | ファミリを指定して、TcpClient クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
TcpClient (IPEndPoint) | TcpClient クラスの新しいインスタンスを初期化し、指定したローカル エンドポイントにバインドします。 .NET Compact Framework によってサポートされています。 |
TcpClient (String, Int32) | TcpClient クラスの新しいインスタンスを初期化し、指定したホストの指定したポートに接続します。 .NET Compact Framework によってサポートされています。 |

TcpClient プロパティ
TcpClient メソッド

名前 | 説明 | |
---|---|---|
![]() | BeginConnect | オーバーロードされます。 リモート ホスト接続への非同期要求を開始します。 |
![]() | Close | 基になる接続を終了せずに、この TcpClient インスタンスを破棄します。 |
![]() | Connect | オーバーロードされます。 指定されたホスト名とポート番号を使用してクライアントをリモート TCP ホストに接続します。 |
![]() | EndConnect | 受信接続の試行を非同期的に受け入れます。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetStream | データの送受信に使用する NetworkStream を返します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Dispose | TcpClient によって使用されているアンマネージ リソースを解放し、オプションでマネージ リソースも解放します。 |
![]() | Finalize | オーバーライドされます。 TcpClient クラスによって使用されていたリソースを解放します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |


TcpClient メンバ
TCP ネットワーク サービスのためのクライアント接続を提供します。
TcpClient データ型で公開されるメンバを以下の表に示します。




名前 | 説明 | |
---|---|---|
![]() | BeginConnect | オーバーロードされます。 リモート ホスト接続への非同期要求を開始します。 |
![]() | Close | 基になる接続を終了せずに、この TcpClient インスタンスを破棄します。 |
![]() | Connect | オーバーロードされます。 指定されたホスト名とポート番号を使用してクライアントをリモート TCP ホストに接続します。 |
![]() | EndConnect | 受信接続の試行を非同期的に受け入れます。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetStream | データの送受信に使用する NetworkStream を返します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Dispose | TcpClient によって使用されているアンマネージ リソースを解放し、オプションでマネージ リソースも解放します。 |
![]() | Finalize | オーバーライドされます。 TcpClient クラスによって使用されていたリソースを解放します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |


- TcpClientのページへのリンク