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.Shutdown Method

Disables sends and receives on a Socket.

[Visual Basic]
Public Sub Shutdown( _
   ByVal how As SocketShutdown _
)
[C#]
public void Shutdown(
 SocketShutdown how
);
[C++]
public: void Shutdown(
 SocketShutdown how
);
[JScript]
public function Shutdown(
   how : SocketShutdown
);

Parameters

how
One of the SocketShutdown values that specifies the operation that will no longer be allowed.

Exceptions

Exception Type Condition
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.

Remarks

When using a connection-oriented Socket, always call the Shutdown method before closing the Socket. This ensures that all data is sent and received on the connected socket before it is closed.

Call the Close method to free all managed and unmanaged resources associated with the Socket. Do not attempt to reuse the Socket after closing.

The following table shows the SocketShutdown enumeration values that are valid for the how parameter.

Value Description
Send Disable sending on this Socket.
Receive Disable receiving on this Socket.
Both Disable both sending and receiving on this Socket.

Setting how to Send specifies that subsequent calls to Send are not allowed. If you are using a connectionless Socket, specifying Send will have no effect.

Setting how to Receive specifies that subsequent calls to Receive are not allowed. This has no effect on lower protocol layers. If you are using a connection-oriented protocol, the connection is terminated if either of the following conditions exist after a call to Shutdown:

  • Data is in the incoming network buffer waiting to be received.
  • More data has arrived.

If you are using a connectionless protocol, datagrams are accepted and queued. However, if no buffer space is available for additional incoming datagrams, they will be discarded and no error will be returned to the sender. Using Shutdown on a connectionless Socket is not recommended.

Setting how to Both disables both sends and receives as described above.

Note   If you receive a SocketException when calling the Shutdown method, 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 uses Shutdown to disable the Socket.

[Visual Basic] 
aSocket.Shutdown(SocketShutdown.Both)
aSocket.Close()
If aSocket.Connected Then
    Console.WriteLine("Winsock error: " _
       + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()))
End If

[C#] 
aSocket.Shutdown(SocketShutdown.Both);
aSocket.Close();
if (aSocket.Connected) {
   Console.WriteLine("Winsock error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()) );
}
   

[C++] 
aSocket->Shutdown(SocketShutdown::Both);
aSocket->Close();
if (aSocket->Connected) {
   Console::WriteLine(S"Winsock error: {0}", Convert::ToString(System::Runtime::InteropServices::Marshal::GetLastWin32Error()) );
}
   

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter 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, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | Close | SocketShutdown

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft