HP X.25/9000 Programmer's Guide

42 Chapter3
Establishing and Terminating a Socket Connection
Terminating a Connection
int sd;
err = close(sd);
sd A socket descriptor for a listen socket or an SVC socket. If there are other
open socket descriptors for the socket, the socket is no longer usable by
the process issuing the call. If there are no other open socket descriptors
for the socket, the socket is destroyed and may not be used again by any
process. If
sd
is the last open socket descriptor and a descriptor for an
SVC socket, a CLEAR REQUEST packet is transmitted on the SVC.
err On successful completion,
err
is set to 0. Otherwise, a value of –1 is
returned and
errno
is set to indicate the error. Even when a non-zero
value is returned,
sd
will not be usable.
The state of the socket and the state of the circuit are independent of one
another. When a socket is closed, it vanishes and can not be accessed
again. When a circuit is cleared and the socket is not closed, it can no
longer transmit data. However, you can read any unread out-of-band
data that arrived on the inbound recv() queues before the clear. Any
unread normal data cannot be read. See Chapter 5 , “Receiving and
Transmitting Out-of-band Information,” for more information on
receiving out-of-band data.
When the last close call is executed on a socket descriptor, any data that
has not yet been sent or received is lost.
The best way to end a session without losing data is summarized below:
1. With a send() call, thesending side sends an “I am finished” message
– the message content is defined by the application designer.
2. The receiving side reads the “I am finished” message with the recv()
call.
3. The receiving side closes the virtual circuit with a close() call.
4. The sending side receives notification of the close() with a
recv(OOB) call.
5. The sending side frees its socket resources by issuing a close() call.
You can specify data and facilities information in the CLEAR packet sent
by the final close(). See “Clearing a Switched Virtual Circuit” for
details.
For syntax and details on close(), refer to the close(2) entry in your
man pages.
36960-90061.bk Page 42 Friday, November 10, 2000 3:42 PM