Socket API Notes
Socket API Notes
• The socket API is an Inter process Communication (IPC) programming interface originally
provided as part of the Berkeley UNIX operating system.
• It has been ported to all modern operating systems, including Sun Solaris and Windows
systems.
• It is a default standard for programming IPC and is the basis of more sophisticated IPC
interface such as remote procedure call and remote method invocation.
• On UNIX-based systems such as BSD or Linux, the API is part of the kernel, or core, of the
operating system.
• Socket is a term borrowed from telephone communications. In the early days, a person
wishing to make a call another person had to go through a human operator, who manually
establishes a connection by physically inserting the two ends of a cable into two specific
receptacles, each assigned to one of the two parties, on a panel of sockets.
• Datagram sockets can support both connectionless and connection-oriented communication at the
application layer. This is so because even though datagrams are sent or received without the notion of
connections at the transport layer, the run-time support of the socket API can create and maintain
logical connections for datagrams exchanged between two processes.
• In Java, two classes are provided for the datagram socket API:
• 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:
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.
Fig 4.2 Connectionless and Connection-oriented Datagram Socket
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.
4.3 The data structures in the sender and receiver programs
Fig 4.4 The program flow in the sender and receiver programs
Figure 4.5(a) illustrates a scenario where a process, A, uses a single connectionless socket to
communicate with two other processes in a session. For example, A may receive a datagram ml from B,
followed by a datagram m2 from C, then m3, m4 from B, followed by m5 from C, and so forth.
Alternatively, it is also possible for A to open a separate socket for each of processes B and C, so that
datagrams from the two processes can be addressed and received via 2 separate sockets as shown in fig
4.5(b)
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.
Figure below shows the event diagram for the execution of programs using a socket.
• The ConnectionAcceptor process starts its execution first. The process is suspended when
the blocking accept method is called, then unsuspended when it receives the connection
request from the Requestor.
• Upon resuming execution, the Acceptor writes a message to the socket before closing both
the data socket and connection socket.
The figure below shows the program flow in a connection listener and connection requestor
in a stream mode socket API.