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

TcpClient コンストラクタ ()

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

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

解説解説
使用例使用例

既定コンストラクタ使用して新しTcpClient作成する方法次のコード例示します

'Creates a TCPClient using the default constructor.
Dim tcpClientC As New TcpClient
//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = new TcpClient ();
//Creates a TCPClient using the default constructor.
TcpClient^ tcpClientC = gcnew TcpClient;

//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = new TcpClient();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
Connect

TcpClient コンストラクタ (String, Int32)

TcpClient クラス新しインスタンス初期化し指定したホスト指定したポート接続します

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

例外例外
例外種類条件

ArgumentNullException

hostnamenull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

port が MinPort と MaxPort の間の値ではありません。

SocketException

ソケットへのアクセス中にエラー発生しました詳細については「解説」を参照してください

解説解説
使用例使用例

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

//Creates a TCPClient using host name and port.
TcpClient tcpClientB = new TcpClient("www.contoso.com",
 11000);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
TcpClient
Connect

TcpClient コンストラクタ (IPEndPoint)

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

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

Public Sub New ( _
    localEP As IPEndPoint _
)
Dim localEP As IPEndPoint

Dim instance As New TcpClient(localEP)
public TcpClient (
    IPEndPoint localEP
)
public:
TcpClient (
    IPEndPoint^ localEP
)
public TcpClient (
    IPEndPoint localEP
)
public function TcpClient (
    localEP : IPEndPoint
)

パラメータ

localEP

TCP Socketバインド先の IPEndPoint。

例外例外
例外種類条件

ArgumentNullException

localEPnull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

ローカル エンドポイント使用して 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);
//Creates a TCPClient using a local end point.
IPAddress^ ipAddress = Dns::Resolve( Dns::GetHostName() )->AddressList[ 0 ];
IPEndPoint^ ipLocalEndPoint = gcnew IPEndPoint( ipAddress,11000 );
TcpClient^ tcpClientA = gcnew TcpClient( ipLocalEndPoint );

//Creates a TCPClient using a local end point.
IPAddress ipAddress =
    Dns.Resolve(Dns.GetHostName()).get_AddressList()[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
TcpClient tcpClientA = new TcpClient(ipLocalEndPoint);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
IPEndPoint クラス
Connect

TcpClient コンストラクタ (AddressFamily)

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

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

Public Sub New ( _
    family As AddressFamily _
)
Dim family As AddressFamily

Dim instance As New TcpClient(family)
public TcpClient (
    AddressFamily family
)
public:
TcpClient (
    AddressFamily family
)
public TcpClient (
    AddressFamily family
)
public function TcpClient (
    family : AddressFamily
)

パラメータ

family

IP プロトコルの AddressFamily。

例外例外
例外種類条件

ArgumentException

family != AddressFamily.InterNetwork

または

family != AddressFamily.InterNetworkV6

解説解説
使用例使用例

TcpClient クラスインスタンス作成する方法次のコード例示します

Dim tcpClientD As New TcpClient(AddressFamily.InterNetwork)
TcpClient tcpClientD = new TcpClient (AddressFamily.InterNetwork);
TcpClient^ tcpClientD = gcnew TcpClient( AddressFamily::InterNetwork );

TcpClient tcpClientD =
    new TcpClient(AddressFamily.InterNetwork);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間

TcpClient コンストラクタ


TcpClient プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Active 接続されたかどうかを示す値を取得または設定します
参照参照

関連項目

TcpClient クラス
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙
IPEndPoint クラス
Connect
Write
Read

その他の技術情報

TCP/UDP

TcpClient メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose TcpClient によって使用されているすべてのリソース解放します。
参照参照

関連項目

TcpClient クラス
System.Net.Sockets 名前空間
TcpListener
NetworkStream クラス
Socket クラス
ProtocolType 列挙
IPEndPoint クラス
Connect
Write
Read

その他の技術情報

TCP/UDP

TcpClient メンバ

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

TcpClient データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Active 接続されたかどうかを示す値を取得または設定します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose TcpClient によって使用されているすべてのリソース解放します。
参照参照

関連項目

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 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS