System.Net.Sockets.Socket.Close Method

Closes the System.Net.Sockets.Socket connection and releases all associated resources with a specified timeout to allow queued data to be sent.

Syntax

public void Close (int timeout)

Parameters

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

Remarks

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

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

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

Note:

To set the SocketOptionName.DontLinger socket option to false, create a System.Net.Sockets.LingerOption, set the enabled property to true, and set the LingerOption.LingerTime property to the desired time-out period. Use this System.Net.Sockets.LingerOption along with the SocketOptionName.DontLinger socket option to call the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0