This post is to help my students, in IV year B.Tech (IT), understand a programming facility for IPC, that is, socket API in Distributed Computing. Socket API provides a low level of abstraction for IPC. This API is appropriate if the resources are limited in a Distributed Environment.
In this post I am going to implement a basic Java Socket program first and then I consider a problem that is solved in a distributed environment with IPC programmed with sockets in java.
In the first application, the job here is to send a message to a receiver node from a sender node where, the message is displayed at the receiver to show that the received message is processed.
The Socket programming implementation of IPC is Asynchronous send and Synchronous receive, because the sender doesn't enter blocking state while sending the message but the receiver will enter into blocking state till the message is received.
The scenario here is, a process Sender is running on a Host A and the other process Receiver is running on Host B.
Code : Sender
---- - ------
import java.net.*;
import java.io.*;
/*
* *************** SOCKET PROGRAMMING (SENDER) **************
* Author : @NaveenKumar
* Date : 29/10/2013
* File Name : Sender.java
*/
public class Sender
{
public static void main(String args[])
{
if(args.length!=3)
{
System.out.println("Insufficient parameters");
}// end of if
else
{
System.out.println("Parameters read");
try{
InetAddress receiverHost=InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String message = args[2];
System.out.println("Arguments copied into variables");
//instantiate a datagram socket for sending the data
DatagramSocket s = new DatagramSocket();
System.out.println("Datagram Socket instantiated");
byte[] buffer = message.getBytes();
DatagramPacket p = new
DatagramPacket(buffer,buffer.length,
receiverHost,receiverPort); System.out.println("Datagram Packet instantiated"); s.send(p);
s.close();
}catch(Exception ex) //end of try and beginning of catch
{
ex.printStackTrace();
}//end of catch
}// end of else
}//end of main
}//end of class
In the above program, the Receiver IP address, Receiver Port number and message to be sent are taken as arguments to main function. Required parameters like Receiver's IP address, Port number, message are set before instantiating the Datagram socket.
After the socket is instantiated (in this case the socket is 's'), a Datagram Packet is instantiated (in this case it is 'p') with the IP address of its receiver, port number and message are bundled into the packet.
Now, the packet is sent through the socket 's', to the receiver socket.Code : Receiver
---- - --------
import java.net.*;
import java.io.*;
/*
* **************** SOCKET PROGRAMMING (RECEIVER) **************
* Author : @NaveenKumar
* Date : 29/10/2013
* File Name : Receiver.java
*/
public class Receiver
{
public static void main(String args[])
{
System.out.println("Starting Receiver");
if(args.length!=1)
{
System.out.println("Insufficient arguments");
}// end of it
else
{
System.out.println("Success on number of arguments");
int receive_port = Integer.parseInt(args[0]);
try
{
DatagramSocket s = new DatagramSocket(receive_port);
System.out.println("Socket created");
byte[] buffer = new byte[10];
System.out.println("buffer created");
DatagramPacket p = new DatagramPacket(buffer,10);
System.out.println("packet created");
s.receive(p);
System.out.println("Received message successfully");
String msg = new String (buffer);
System.out.println(msg);
s.close();
}catch(Exception ex) // end of try and start of catch
{
ex.printStackTrace();
}// end of catch
}// end of else
}// end of main
}// end of class Receiver
In this case of the Receiver process running on Host B, a Datagram Socket is instantiated to facilitate the receive operation, a Datagram Packet is instatitated and when the receive operation is reached, the process is sent to blocked state and set to wait for the message to be received from the Sender.
To show the blocking, checksums are provided at each stage. Once the receive operation is completed, the process is unblocked and continued with the processing of data.
The communication in the above scenario is between two processes Sender and Receiver performing either send or receive operations respectively but any process can send as well as receive data to/from other processes running on same or different hosts. That may be illustrated in the next post.
Hope this is helpful.
No comments:
Post a Comment