TcpClient.Close メソッド
アセンブリ: System (system.dll 内)


Close メソッドは、インスタンスを破棄されたとしてマークしますが、TCP 接続は終了しません。このメソッドを呼び出しても、データの送受信に使用される NetworkStream は解放されません。Close メソッドを呼び出して、ストリームと TCP 接続を終了する必要があります。
![]() |
---|
このメンバは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。詳細については、「ネットワークのトレース」を参照してください。 |

Close メソッドを呼び出すことによって TcpClient を終了するコード例を次に示します。
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace Examples.System.Net { public class TCPClientExample { public static void Main() { // Create a client that will connect to a // server listening on the contosoServer computer // at port 11000. TcpClient tcpClient = new TcpClient(); tcpClient.Connect("contosoServer", 11000); // Get the stream used to read the message sent by the server. NetworkStream networkStream = tcpClient.GetStream(); // Set a 10 millisecond timeout for reading. networkStream.ReadTimeout = 10; // Read the server message into a byte buffer. byte[] bytes = new byte[1024]; networkStream.Read(bytes, 0, 1024); //Convert the server's message into a string and display it. string data = Encoding.UTF8.GetString(bytes); Console.WriteLine("Server sent message: {0}", data); networkStream.Close(); tcpClient.Close(); } } }
#using <System.dll> using namespace System; using namespace System::Text; using namespace System::Net; using namespace System::Net::Sockets; int main() { // Create a client that will connect to a // server listening on the contoso1 computer // at port 11000. TcpClient^ tcpClient = gcnew TcpClient; tcpClient->Connect( "contosoServer", 11000 ); // Get the stream used to read the message sent by the server. NetworkStream^ networkStream = tcpClient->GetStream(); // Set a 10 millisecond timeout for reading. networkStream->ReadTimeout = 10; // Read the server message into a byte buffer. array<Byte>^bytes = gcnew array<Byte>(1024); networkStream->Read( bytes, 0, 1024 ); //Convert the server's message into a string and display it. String^ data = Encoding::UTF8->GetString( bytes ); Console::WriteLine( "Server sent message: {0}", data ); networkStream->Close(); tcpClient->Close(); }

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.Close メソッドを検索する場合は、下記のリンクをクリックしてください。

- TcpClient.Close メソッドのページへのリンク