HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 3 Advanced Topics for Stream Sockets

Using Shutdown

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

When your program is done reading or writing on a particular socket connection, you can use shutdown to bring down a part of the connection.

When one process uses shutdown on a socket descriptor, all other processes with the same socket descriptor are affected. shutdown causes all or part of a full-duplex connection on the specified socket to be disabled. When shutdown is executed, the specified socket is marked unable to send or receive, according to the value of how.

  • If how = 0, the specified socket can no longer receive data. The connection is not completely down until both sides have done a shutdown or a close.

  • If how = 1, shutdown starts a graceful disconnect by attempting to send any unsent data before preventing further sending. shutdown sends an end-of-file condition to the peer, indicating that there are no more data to be sent.

    Once both shutdown(s, 0) and shutdown(s, 1) have been executed on the same socket descriptor, the only valid operation on the socket at this point is a close.

  • If how = 2, the specified socket can no longer send or receive data. The only valid operation on the socket is a close. This has the same effect as executing shutdown(s, 0) and shutdown(s, 1) on the same socket descriptor.

If you use close on a socket, close pays attention to the SO_LINGER option, but shutdown(s, 2) does not. With close, the socket descriptor is deallocated and the last process using the socket destroys it. shutdown and its parameters are described in the following table.

Include files:

none

System call:

shutdown(s,how) 
int s, how;

Parameter

Description of Contents

INPUT Value

s

socket descriptor

socket descriptor of socket to be shut down

how

number that indicates the type of shutdown

0, 1 or 2

Function result:

0 if shutdown is successful, -1 if failure occurs.

Example:
shutdown (s, 1);

When to Shut Down a Socket

Optionally, after the process has sent all messages and wants to indicate that it is done sending, shut down the server or client process. Refer to the shutdown(2) man page for more information on shutdown.

© 1997 Hewlett-Packard Development Company, L.P.