When you have finished using a socket, you can simply close its
file descriptor with
close; see Opening and Closing Files.
If there is still data waiting to be transmitted over the connection,
close tries to complete this transmission. You
can control this behavior using the
SO_LINGER socket option to
specify a timeout period; see Socket Options.
You can also shut down only reception or transmission on a
connection by calling
shutdown, which is declared in
shutdownfunction shuts down the connection of socket socket. The argument how specifies what action to perform:
- Stop receiving data for this socket. If further data arrives, reject it.
- Stop trying to transmit data from this socket. Discard any data waiting to be sent. Stop looking for acknowledgement of data already sent; don't retransmit it if it is lost.
- Stop both reception and transmission.
The return value is
0on success and
-1on failure. The following
errnoerror conditions are defined for this function:
- socket is not a valid file descriptor.
- socket is not a socket.
- socket is not connected.