TcpClient.Close TcpClient.Close TcpClient.Close TcpClient.Close Method

定义

释放此 TcpClient 实例,并请求关闭基础 TCP 连接。Disposes this TcpClient instance and requests that the underlying TCP connection be closed.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

示例

下面的代码示例演示如何TcpClient通过Close调用方法来关闭。The following code example demonstrates closing a TcpClient by calling the Close method.

#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();
}

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();
		}
    }
}

注解

方法将实例标记为已释放, 并请求关联Socket的关闭 TCP 连接。 CloseThe Close method marks the instance as disposed and requests that the associated Socket close the TCP connection. 基于属性, TCP 连接可能在一段时间Close后保持打开状态, 以便在数据继续发送时调用方法。 LingerStateBased on the LingerState property, the TCP connection may stay open for some time after the Close method is called when data remains to be sent. 基础连接完成结束时, 未提供任何通知。There is no notification provided when the underlying connection has completed closing.

调用此方法最终将导致关联Socket的关闭, 并且还会关闭用于发送和接收数据的关联NetworkStream (如果已创建)。Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于