programming-examples/java/IPC Client Server Program.java
2019-11-18 13:43:20 +01:00

70 lines
2.6 KiB
Java

// IPC CLient Program
import java.net.*;
import java.io.*;
public class IPCClient
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",3128);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
InputStreamReader isr=new InputStreamReader(System.in);
System.out.println("......Client PROCESS STARTED....");
System.out.println("Enter the value of 1 and 2 to pass the server process");
BufferedReader br=new BufferedReader(isr);
int a=Integer.parseInt(br.readLine());
System.out.println("\Number 1="+a);
dos.writeInt(+a);
int b=Integer.parseInt(br.readLine());
System.out.println("\Number 2="+b);
dos.writeInt(+b);
int result=dis.readInt();
System.out.println("Client Process has receieved result from server\n..");
s.close();
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
//IPC Server
import java.net.*;
import java.io.*;
public class IPCServer
{
public static void main(String args[])
{
System.out.println("......INTERPROCESS COMMUNICATION....");
System.out.println("......SERVER PROCESS STARTED....");
System.out.println("......SERVER IS READY AND WAITING TO RECEIVE DATA FROM CLIENT FROM PORT...."+3128);
try
{
ServerSocket ss=new ServerSocket(3128);
Socket clientSocket=ss.accept();
System.out.println("Client is connected with IP Address"+clientSocket.getInetAddress()+"And Port no"+clientSocket.getPort());
DataOutputStream dos=new DataOutputStream(clientSocket.getOutputStream());
DataInputStream dis=new DataInputStream(clientSocket.getInputStream());
int a=dis.readInt();
System.out.println("Server recievd");
System.out.println("\Number 1="+a);
int b=dis.readInt();
System.out.println("\Number 2="+b);
int c=a+b;
dos.writeInt(c);
System.out.println("Server Process has executed..Requested process and sent result"+c+"To the Client\n");
clientSocket.close();
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}