TcpClient.Close Method
Closes the TCP connection and releases all resources associated with the TcpClient.
[Visual Basic] Public Sub Close() [C#] public void Close(); [C++] public: void Close(); [JScript] public function Close();
Exceptions
| Exception Type | Condition |
|---|---|
| SocketException | An error occurred when accessing the socket. See the Remarks section for more information. |
Remarks
The Close method closes the TCP connection. It calls the Dispose method passing a true value to release all managed and unmanaged resources associated with the TcpClient. These resources include the underlying Socket used for connecting with the remote host, and the NetworkStream used to send and receive data.
Note If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.
Example
[Visual Basic, C#, C++] The following example demonstrates closing a TcpClient by calling the Close method.
[Visual Basic] Dim tcpClientD As New TcpClient(AddressFamily.InterNetwork) ' Uses the Close public method to close the network stream and socket. tcpClient.Close() End Sub 'MyTcpClientCommunicator [C#] TcpClient tcpClientD = new TcpClient (AddressFamily.InterNetwork); [C++] TcpClient* tcpClientD = new TcpClient(AddressFamily::InterNetwork);
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
See Also
TcpClient Class | TcpClient Members | System.Net.Sockets Namespace | Close | Shutdown