gtpc1m3b | Transmission Control Protocol/Internet Protocol |
In a TCP connected stream socket session, the roles of server and client
are more clearly defined than in a datagram socket session. Once you
make the connection, the connection exists until you close the socket.
A connected socket, as used in TCP protocol, sends data to and receives data
from only one server because it has a dedicated destination. The
following steps correspond to the numbers in Figure 23:
- The server is started first by issuing a socket call to create
socket s. The client then issues a socket call
and creates its own socket s. A socket is initially created
in the unconnected state, which means that the socket is not associated with
any remote destination.
- The server issues a bind call to a local address to be
positioned for a subsequent connection.
The client can issue an optional bind call to a local
address.
- Using the listen function, the server waits for a connection
from the client.
- The connect call places the socket in the connected
state. The client must issue the connect call
before being able to transfer data through a reliable stream socket.
- The server issues an accept call to accept an incoming
connection. To allow the server socket s to remain available
for the next client connection, the accept call creates a new
socket ns, which is dedicated to the client.
- The read and write calls between the client and
server continue until all the data is transferred.
- The client closes socket s and the server closes the related
socket, socket ns.
- The server can continue to accept other connections on the original socket
s or close it by using the close function.
Figure 23. Sample Socket Session Using TCP Protocol