Concept:
In this practical make use of socket programming. On the client side design an application that will give the list of commands. The client will invoke these commands. On the server side these commands will be executed. The implementation of the above commands will be on the server side.
a. Implementation of stack
Commands: PUSH, POP, DISPLAY, EXIT, etc.
b. Implementation of queue.
Commands: INSERT, DELETE, DISPLAY, EXIT, etc.
a. Implementation of Stack
-----------------------------------------------------------------------------------------------------------
SOURCE CODE
-----------------------------------------------------------------------------------------------------------
Server Program :
import java.io.*;
import java.net.*;
//class implementing Stack operations
class Stack{
private int stck[];
private int tos;
// allocate and initialize stack
Stack(int size){
stck = new int[size];
tos = -1;
}
//check whether stack is full
boolean isFull(){
if(tos == stck.length-1)
return true;
else
return false;
}
//check whether stack is empty
boolean isEmpty(){
if(tos < 0)
return true;
else
return false;
}
// Push an item onto the stack
void push(int item){
if(! (tos==stck.length-1)) // use length member
stck[++tos] = item;
}
// Pop an item from the stack
int pop(){
return stck[tos--];
}
//get entire stack
int[] getStack(){
return stck;
}
//get current size of the stack
int getCurrentSize(){
return (tos+1);
}
}
//class implementing Stack Server operations
class StackServer implements Runnable{
static ServerSocket serverSocket;
Socket socket;
BufferedReader read,from;
PrintWriter to;
Thread thread;
Stack stack;
int clientID;
static int clientCounter=0;
//constructor
StackServer(Socket socket)throws Exception{
this.socket=socket;
clientID=++clientCounter;
System.out.println("New Client connection accepted. ClientID: "+clientID);
from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
to=new PrintWriter(socket.getOutputStream(),true);
thread=new Thread(this);
thread.start();
}
//run() method for each client request thread
public void run(){
try{
to.println("Enter size of the stack: ");
int size=Integer.parseInt((from.readLine()).trim());
stack=new Stack(size);
int item=0;
String action=" ";
to.println("You can \"push\" \"pop\" \"display\" elements or \"exit\" What next??");
do{
action=(from.readLine()).toLowerCase();
if(action.equals("push")){
if(stack.isFull())
to.println("Stack Full. What next??");
else{
to.println("Enter element: ");
item=Integer.parseInt((from.readLine()).trim());
stack.push(item);
to.println("Pushed item. What next??");
}
}
else if(action.equals("pop")){
if(stack.isEmpty())
to.println("Stack Empty. What next??");
else
to.println("Popped item: "+stack.pop()+" What next??");
}
else if(action.equals("display")){
if(stack.isEmpty())
to.println("Stack Empty. What next??");
else{
int[] stck=stack.getStack();
String stckStr="";
for(int i=0;i
to.println("Stack contains: "+stckStr+" What next??");
}
}
else if(action.equals("exit")){
socket.close();
break;
}
else
to.println("Invalid option. What next??");
}while(!(action=="exit"));
System.out.println("Client "+clientID+" exited.");
}catch(Exception e){System.out.println(e);}
}
//main() method
public static void main(String args[])throws Exception{
serverSocket=new ServerSocket(2222);
System.out.println("Server Started. Press Ctrl+C to close server.");
while(true){
Socket s=serverSocket.accept();
StackServer server=new StackServer(s);
}
}
}
Client Program :
import java.io.*;
import java.net.*;
//class implementing Stack Client operations
class StackClient{
//main() method
public static void main(String args[]){
try{
String in="";
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter server address: ");
Socket socket=new Socket(read.readLine(),2222);
BufferedReader from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter to=new PrintWriter(socket.getOutputStream(),true);
//read write loop
while(true){
System.out.println(from.readLine());
in=read.readLine();
to.println(in);
if(in.equalsIgnoreCase("exit")){
socket.close();
break;
}
}
System.out.println("Client closing.");
}catch(Exception e){e.printStackTrace(System.out);}
}
}
No comments:
Post a Comment