TcpClient.Close Method

Disposes this TcpClient instance and requests that the underlying TCP connection be closed.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public void Close()

The Close method marks the instance as disposed and requests that the associated Socket close the TCP connection. 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. There is no notification provided when the underlying connection has completed closing.

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.

NoteNote:

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following code example demonstrates closing a TcpClient by calling the Close method.

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 <mscorlib.dll>
#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 = new TcpClient();
    tcpClient->Connect(S"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(S"Server sent message: {0}", data);
    networkStream->Close();
    tcpClient->Close();
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft