DS Experiment no 6


Aim - Implementation of Mutual Exclusion algorithms.

Implementation of Mutual Exclusion algorithms.
Implementation of Mutual Exclusion algorithms.


Server.java
import java.io.*;
import java.net.*;
public class EchoServer implements Runnable
{
   Socket socket=null;
   static ServerSocket ss;
   EchoServer(Socket newSocket)
   {
       this.socket=newSocket;        
   }
   public static void main(String args[]) throws IOException
   {
       ss=new ServerSocket(7000);
       System.out.println("Server Started");
       while(true)
       {
           Socket s = ss.accept();
           EchoServer es = new EchoServer(s);
           Thread t = new Thread(es);
           t.start();
       }}
   public void run()  {
       try {        
           BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
           while(true) {
               System.out.println(in.readLine());
           }}
       catch(Exception e)
       {}}}
Output of server
D:\Java\jdk1.8.0_111\bin>javac EchoServer.java
D:\Java\jdk1.8.0_111\bin>java EchoServer
Server Started
hey!!!!\
hello




ClientOne.java
import java.io.*;
import java.net.*;
public class EchoClientOne
{
   public static void main(String args[])throws IOException
   {
       Socket s=new Socket("localhost",7000);
       PrintStream out = new PrintStream(s.getOutputStream());
       ServerSocket ss = new ServerSocket(7001);
       Socket s1 = ss.accept();
       BufferedReader in1 = new BufferedReader(new
InputStreamReader(s1.getInputStream()));
       PrintStream out1 = new PrintStream(s1.getOutputStream());
       BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
       String str="Token";
       while(true)
       {
           if(str.equalsIgnoreCase("Token"))
           {
               System.out.println("Do you want to send some data");
               System.out.println("Enter Yes or No");
               str=br.readLine();
               if(str.equalsIgnoreCase("Yes"))
               {
                   System.out.println("Enter the data");
                   str=br.readLine();
                   out.println(str);}
               out1.println("Token");}
           System.out.println("Waiting for Token");
           str=in1.readLine();
       }}}
Output:
D:\Java\jdk1.8.0_111\bin>javac EchoClientOne.java
D:\Java\jdk1.8.0_111\bin>java EchoClientOne
Do you want to send some data
Enter Yes or No
yes
Enter the data
hey!!!!\
Waiting for Token
ClientTwo.java
import java.io.*;
import java.net.*;
public class EchoClientTwo
{
   public static void main(String args[])throws IOException
{    Socket s=new Socket("localhost",7000);
       PrintStream out = new PrintStream(s.getOutputStream());
       Socket s2=new Socket("localhost",7001);
       BufferedReader in2 = new BufferedReader(new
InputStreamReader(s2.getInputStream()));
       PrintStream out2 = new PrintStream(s2.getOutputStream());
       BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
       String str;
       while(true)
       {   System.out.println("Waiting for Token");
           str=in2.readLine();
           if(str.equalsIgnoreCase("Token"))
           {  System.out.println("Do you want to send some data");
               System.out.println("Enter Yes or No");
               str=br.readLine();
               if(str.equalsIgnoreCase("Yes"))
               {   System.out.println("Enter the data");
                   str=br.readLine();
                   out.println(str); }
               out2.println("Token");            }}}}

Output
D:\Java\jdk1.8.0_111\bin>javac EchoClientTwo.java
D:\Java\jdk1.8.0_111\bin>java EchoClientTwo
Waiting for Token
Do you want to send some data
Enter Yes or No
no
Waiting for Token
Do you want to send some data
Enter Yes or No
yes
Enter the data
hello

Waiting for Token



Click here to download
DS Experiment no 6 DS Experiment no 6 Reviewed by akshay salve on 9:04 PM Rating: 5

No comments:

Powered by Blogger.