The Socket API
The Socket API
Introduction
Borrowing from the terminology of telephony, the designer of the socket has
provided a programming construct termed a socket. A process wishing to
communicate with another process must create an instance of such a construct
(see Figure 4.1). Unlike early telephony, however, the communication between
the parties may be connection-oriented or connectionless.
• There are two key protocols at the transport layer of the Internet architecture: User
Datagram Protocol (UDP) and Transmission Control Protocol (TCP).
• The User Datagram Protocol (UDP) allows a packet to be transported using
connectionless communication. The data packet thus transported is called
a datagram. In accordance with connectionless communication, each datagram
transported is individually addressed and routed, and may arrive at the receiver in
any order.
• The Transmission Control Protocol (TCP) is connection-oriented and transports a
stream of data over a logical connection established between the sender and the
receiver.
The Java socket API, as with all other socket APIs, provides socket programming
constructs that make use of either the UDP or TCP protocol. Sockets that use
UDP for transport are known as datagram sockets, while sockets that use TCP
are termed stream sockets.
• In Java, two classes are provided for the datagram socket API:
1. The DatagramSocket class for the sockets
2. The DatagramPacket class for the datagrams exchanged
• A process wishing to send or receive data using this API must instantiate
a DatagramSocket object or a socket in short. Each socket is said to
be bound to a UDP port of the machine local to the process.
• To send a datagram to another process, a process creates an object that
represents the datagram itself. This object can be created by instantiating
a DatagramPacket object which carries:
1. the payload data as a reference to a byte array, and
2. the destination address (the host ID and port number to which the receiver’s
socket is bound).
• Once the DatagramPacket object is created and loaded with payload data and
destination, the sending process then invokes a call to the send method in
the DatagramSocket object, specifying a reference to
the DatagramPacket object as an argument.
• In the receiving process, a DatagramSocket object must also be instantiated and
bound to a local port, the port number must agree with that specified in the
datagram packet of the sender.
• To receive datagrams sent to the socket, the process creates
a DatagramPacket object which references a byte array and calls
a receive method in its DatagramSocket object, specifying as argument a
reference to the DatagramPacket object.
Figure 4.3 illustrates the data structures in the programs for the two processes,
while figure 4.4 illustrates the program flow in the two processes. With
connectionless sockets, a socket bound to a process can be used to datagrams
to different destinations. It is also possible for multiple processes to
simultaneously send datagrams to the same socket bound to a receiving process,
in which case the order of the arrival of these messages will be unpredictable, in
accordance with the underlying UDP protocol.
Fig 4.4 The program flow in the sender and receiver programs
• In Java, the stream-mode socket API is provided with two classes:
– Server socket: for accepting connections; we will call an object of this class a
connection socket.
– Socket: for data exchange; we will call an object of this class a data socket.
• Using this API, a process known as the server establishes a connection socket
and then listens for connection requests from other processes.
• Connection requests are accepted one at a time. When a connection is
accepted, a data socket is created for the connection.
• A process that wishes to communicate with the server is known as a client. A
client creates a socket; then, via the server's connection socket, it makes a
request to the server for a connection.
• Once the request is accepted, the client's socket is connected to the server's
data socket so that the client may proceed to read from and/or write to the data
stream.
• When the communication session between the two processes is over, the data
socket is closed, and the server is free to accept the next connection request.