Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.Close Method (Int32)

Note: This method is new in the .NET Framework version 2.0.

Closes the Socket connection and releases all associated resources.

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

public void Close (
	int timeout
)
public void Close (
	int timeout
)
public function Close (
	timeout : int
)

Parameters

timeout

Wait up to timeout seconds to send any remaining data, then close the socket.

The Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. Upon closing, the Connected property is set to false.

For connection-oriented protocols, it is recommended that you call Shutdown before calling Close. This ensures that all data is sent and received on the connected socket before it is closed.

If you need to call Close without first calling Shutdown, you can ensure that data queued for outgoing transmission will be sent by setting the DontLinger option to false and specifying a non-zero time-out interval. Close will then block until this data is sent or until the specified time-out expires. If you set DontLinger to false and specify a zero time-out interval, Close releases the connection and automatically discards outgoing queued data.

NoteNote

To set the DontLinger socket option to false, create a LingerOption, set the enabled property to true, and set the LingerTime property to the desired time-out period. Use this LingerOption along with the DontLinger socket option to call the SetSocketOption method.

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 how to close a Socket.

aSocket.Shutdown(SocketShutdown.Both);
aSocket.Close();

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

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

Community Additions

Show:
© 2014 Microsoft