0% found this document useful (0 votes)
38 views4 pages

Experiment No: 09 Mutual Exclusion: Code: Echoclientone - Java

This document describes an experiment on mutual exclusion using three Java programs - EchoClientOne, EchoClientTwo, and EchoServer. EchoClientOne and EchoClientTwo act as client programs that communicate with each other and EchoServer using sockets to exchange a "Token" string, allowing only one client access to EchoServer at a time in order to avoid conflicting access. EchoServer is a multithreaded server program that handles client connections concurrently and prints any data received to standard output.

Uploaded by

Harsh Waghela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Experiment No: 09 Mutual Exclusion: Code: Echoclientone - Java

This document describes an experiment on mutual exclusion using three Java programs - EchoClientOne, EchoClientTwo, and EchoServer. EchoClientOne and EchoClientTwo act as client programs that communicate with each other and EchoServer using sockets to exchange a "Token" string, allowing only one client access to EchoServer at a time in order to avoid conflicting access. EchoServer is a multithreaded server program that handles client connections concurrently and prints any data received to standard output.

Uploaded by

Harsh Waghela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment No: 09

Mutual Exclusion
Code:
EchoClientOne.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();
}
}
}
EchoClientTwo.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");
}
}
}
}
EchoServer.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:

You might also like