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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTcpClient クラスを検索する場合は、下記のリンクをクリックしてください。

- TcpClient クラスのページへのリンク