TcpClient.Close Método

Definición

Elimina esta instancia TcpClient y solicita que se cierre la conexión TCP subyacente.

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

Ejemplos

En el ejemplo de código siguiente se muestra cómo cerrar mediante una TcpClient llamada al Close método .

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

Comentarios

El Close método marca la instancia como desechada y solicita que el asociado Socket cierre la conexión TCP. En función de la LingerState propiedad , la conexión TCP puede permanecer abierta durante algún tiempo después de llamar al Close método cuando los datos permanecen en enviarse. No se proporciona ninguna notificación cuando se ha completado el cierre de la conexión subyacente.

Si se llama a este método, se cerrará el asociado Socket y también se cerrará el asociado NetworkStream que se usa para enviar y recibir datos si se creó uno.

Nota

Este miembro genera información de seguimiento cuando se habilita el seguimiento de red en la aplicación. Para obtener más información, consulte Seguimiento de red en la .NET Framework.

Se aplica a