A) IMPLEMENTATION OF STACK
/*Server Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stser
{
//Declaration of variables
ServerSocket ss=null;
String stack[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on stack
public void check()
{
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
System.out.println("Connection Established......");
s=ss.accept();
while(true)
{//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);
if(x==1) //For the Push operation on stack
{
System.out.println("Push");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(stack[i]=="")
break;
}
if(stack[4]!="")
System.out.println("Stack full...");
else
{
System.out.println("Push Operation Performed");
stack[i]=str;
}
}
if(x==2) //For the Pop operation on stack
{
System.out.println("Pop");
for(i=4;i>=0;i--)
{
if(stack[i]!="")
break;
}
System.out.println("Pop Operation Performed-" +stack[i]);
stack[i]="";
}
if(x==3)
//returns the values to the client
{//Output stream that contains print( ) and println( )
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(stack[i]);
pw.flush();
}
}
if(x==4) //terminates the program
{
System.exit(0);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class StackServer //class which contains the main method
{
public static void main(String args[])
{
stser st=new stser();
st.check();
}
}
/* Client Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stclient
{
//Declaration of variables
Socket s=null;
String stack[]=new String[5];
String st;
String str;
int i,x;
public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Push");
System.out.println("2 Pop");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}
public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Push();//push operation
}
if(x==2)
{
Values(); // pop operation
}
if(x==3)
{
Display (); // displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Push() throws IOException
{
System.out.print("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in Stack");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
stack[i]=br2.readLine();
System.out.println(stack[i]);
}
Values();
}
}
class StackClient
{
public static void main(String args[]) throws IOException
{
stclient cl=new stclient();
cl.check();
cl.Contents();
}
}
No comments:
Post a Comment