TcpClient.Close Metodo

Definizione

Elimina questa istanza di TcpClient e richiede che la connessione TCP sottostante venga chiusa.

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

Esempio

Nell'esempio di codice seguente viene illustrata la chiusura di un TcpClient oggetto chiamando il Close metodo .

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

Commenti

Il Close metodo contrassegna l'istanza come eliminata e richiede che l'oggetto associato Socket chiuda la connessione TCP. In base alla LingerState proprietà , la connessione TCP può rimanere aperta per un certo periodo di tempo dopo la chiamata del Close metodo quando i dati rimangono da inviare. Al termine della chiusura della connessione sottostante non viene fornita alcuna notifica.

La chiamata a questo metodo genererà infine la chiusura dell'oggetto associato Socket e chiuderà anche l'oggetto associato NetworkStream utilizzato per inviare e ricevere dati se ne è stato creato uno.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete nel .NET Framework.

Si applica a