TcpClient クラスとは? わかりやすく解説

TcpClient クラス

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

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Class TcpClient
    Implements IDisposable
public class TcpClient implements IDisposable
public class TcpClient implements IDisposable
解説解説
使用例使用例

TcpClient 接続確立するコード例次に示します

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
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
  System.Net.Sockets.TcpClient
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient メンバ
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙
IPEndPoint クラス
Connect
Write
Read
その他の技術情報
TCP/UDP


このページでは「.NET Framework クラス ライブラリ リファレンス」からTcpClient クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTcpClient クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTcpClient クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「TcpClient クラス」の関連用語

TcpClient クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TcpClient クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS