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

Definición

Elimina esta instancia TcpClient y solicita que se cierre la conexión TCP subyacente.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 ()

Ejemplos

En el ejemplo de código siguiente se muestra cómo cerrar una TcpClient mediante una llamada a la Close método.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();
		}
    }
}

Comentarios

El Close método marca la instancia como eliminada y solicita que el asociado Socket cerrar la conexión TCP.The Close method marks the instance as disposed and requests that the associated Socket close the TCP connection. Según el LingerState propiedad, la conexión TCP puede permanecer abierta durante algún tiempo después de la Close método se llama cuando se envíen los datos permanecen.Based 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. No hay ninguna notificación proporcionada cuando se haya completado cierre la conexión subyacente.There is no notification provided when the underlying connection has completed closing.

Llamar a este método producirá finalmente el cierre del asociado Socket y también se cerrará asociado NetworkStream que se utiliza para enviar y recibir datos si se ha creado uno.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.

Nota

Este miembro genera información de seguimiento cuando se habilita el seguimiento de red en la aplicación.This member outputs trace information when you enable network tracing in your application. Para obtener más información, consulte seguimiento de red en .NET Framework.For more information, see Network Tracing in the .NET Framework.

Se aplica a