Wednesday, 19 September 2012


 Simulating Network Operating System (NOS) commands

On the client side design an application that will display a list of commands. The client will invoke these commands. On the server side these commands will be executed.

1.Implementation of Stack

Commands: PUSH, POP, DISPLAY and EXIT

2.Implementation of Queue

Commands: INSERT, DELETE, DISPLAY and EXIT

3. Implementation of Linked List


Stack

Client.java

import java.io.*;
import java.net.*;
public class Client
{

public static void main(String args[]) throws Exception
{
int option;
String strClientMsg,strServerMsg;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Socket s=new Socket("localhost",1111);

BufferedReader frmServer=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toServer=new PrintStream(s.getOutputStream());

do
{
System.out.println("\nPerform Stack Operations : \n");
System.out.println("1. PUSH");
System.out.println("2. POP");
System.out.println("3. DISPLAY");
System.out.println("4. QUIT");
System.out.println("\nEnter your option (1, 2 ,3 or 4) - ");

while(true)
{
try{
strClientMsg=br.readLine();
option=Integer.parseInt(strClientMsg);
break;
}catch(NumberFormatException e)
{
System.out.println("*** Invalid Option : Put an Integer value ***");
continue;
}
}

toServer.println(strClientMsg);
toServer.flush();

switch(option)
{
case 1:
System.out.println("\nEnter element(Data) to PUSH in stack : ");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
break;

case 2:
strServerMsg=frmServer.readLine();
System.out.println(strServerMsg);
break;

case 3:
System.out.println("\nYour Stack Contains :");
strServerMsg=frmServer.readLine();
System.out.println("\n"+strServerMsg);
break;

case 4:
s.close();
break;

default :
System.out.println("\n***  Invalid Option  ***.");
break;
}

if(option!=4)
{
System.out.println("\nDo u want to continue (y/n)");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
}

}while(strClientMsg.equalsIgnoreCase("y"));
System.out.println("\n\nThank you");
}
}

Server.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(1111);
System.out.println("\nServer is Instantiated.");

int option;
String strClientMsg;
List<String> myStack=new ArrayList<String>();

while(true)
{
Socket s=ss.accept();
System.out.println("\nConnection Established.");

BufferedReader frmClient=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toClient=new PrintStream(s.getOutputStream());

do
{
strClientMsg=frmClient.readLine();
option=Integer.parseInt(strClientMsg);

switch(option)
{
case 1:
strClientMsg=frmClient.readLine();
myStack.add(strClientMsg);
break;

case 2:
if(!myStack.isEmpty())
{
String strRemovedElement=myStack.get(myStack.size()-1);
myStack.remove(myStack.size()-1);
toClient.println("Using LIFO rule : '"+strRemovedElement+"' element is POPED from Stack.");
toClient.flush();
}
else
{
toClient.println("Sorry : Stack is Empty.");
toClient.flush();
}

break;

case 3:
toClient.println(myStack);
toClient.flush();
break;

case 4:
s.close();
break;
}

strClientMsg="";
if(option!=4)
{
strClientMsg=frmClient.readLine();
}

}while(strClientMsg.equalsIgnoreCase("y"));
}
}
}

Output:






Queue

Client.java

import java.io.*;
import java.net.*;
public class Client
{

public static void main(String args[]) throws Exception
{
int option;
String strClientMsg,strServerMsg;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Socket s=new Socket("localhost",1111);

BufferedReader frmServer=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toServer=new PrintStream(s.getOutputStream());

do
{
System.out.println("\nPerform Queuing Operations : \n");
System.out.println("1. INSERT/ENQUEUE");
System.out.println("2. DELETE/DEQUEUE");
System.out.println("3. DISPLAY");
System.out.println("4. QUIT");
System.out.println("\nEnter your option (1, 2 ,3 or 4) - ");

while(true)
{
try{
strClientMsg=br.readLine();
option=Integer.parseInt(strClientMsg);
break;
}catch(NumberFormatException e)
{
System.out.println("*** Invalid Option : Put an Integer value ***");
continue;
}
}

toServer.println(strClientMsg);
toServer.flush();

switch(option)
{
case 1:
System.out.println("\nEnter element(Data) to INSERT/ENQUEUE in Queue : ");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
break;

case 2:
strServerMsg=frmServer.readLine();
System.out.println(strServerMsg);
break;

case 3:
System.out.println("\nYour Queue Contains :");
strServerMsg=frmServer.readLine();
System.out.println("\n"+strServerMsg);
break;

case 4:
s.close();
break;

default :
System.out.println("\n***  Invalid Option  ***.");
break;
}

if(option!=4)
{
System.out.println("\nDo u want to continue (y/n)");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
}

}while(strClientMsg.equalsIgnoreCase("y"));
System.out.println("\n\nThank you");
}
}

Server.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(1111);
System.out.println("\nServer is Instantiated.");

int option;
String strClientMsg;
List<String> myQueue=new ArrayList<String>();

while(true)
{
Socket s=ss.accept();
System.out.println("\nConnection Established.");

BufferedReader frmClient=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toClient=new PrintStream(s.getOutputStream());

do
{
strClientMsg=frmClient.readLine();
option=Integer.parseInt(strClientMsg);

switch(option)
{
case 1:
strClientMsg=frmClient.readLine();
myQueue.add(strClientMsg);
break;

case 2:
if(!myQueue.isEmpty())
{
String strRemovedElement=myQueue.get(0);
myQueue.remove(0);
toClient.println("Using FIFO rule : '"+strRemovedElement+"' element is DELETED/DEQUEUED from Queue.");
toClient.flush();
}
else
{
toClient.println("Sorry : Queue is Empty.");
toClient.flush();
}

break;

case 3:
toClient.println(myQueue);
toClient.flush();
break;

case 4:
s.close();
break;
}

strClientMsg="";
if(option!=4)
{
strClientMsg=frmClient.readLine();
}

}while(strClientMsg.equalsIgnoreCase("y"));
}
}
}

Output:






LinkedList

Client.java

import java.io.*;
import java.net.*;
public class Client
{

public static void main(String args[]) throws Exception
{
int option;
String strClientMsg,strServerMsg;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Socket s=new Socket("localhost",1111);

BufferedReader frmServer=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toServer=new PrintStream(s.getOutputStream());

do
{
System.out.println("\nPerform Linklist Operations with on an Element: \n");

System.out.println("1. INSERT AT BEGINNING");
System.out.println("2. INSERT IN MIDDLE ");
System.out.println("3. INSERT AT END");
System.out.println("4. DELETE FROM BEGINNING");
System.out.println("5. DELETE FROM MIDDLE");
System.out.println("6. DELETE FROM END");
System.out.println("7. DISPLAY");
System.out.println("8. QUIT");
System.out.println("\nEnter your option (1, 2 , 3, 4, 5, 6, 7 or 8) - ");

while(true)
{
try{
strClientMsg=br.readLine();
option=Integer.parseInt(strClientMsg);
break;
}catch(NumberFormatException e)
{
System.out.println("*** Invalid Option : Put an Integer value ***");
continue;
}
}

toServer.println(strClientMsg);
toServer.flush();

switch(option)
{
case 1:
System.out.println("\nEnter element(Data) to INSERT AT BEGINNING in Linklist : ");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
break;

case 2:
strServerMsg=frmServer.readLine();
System.out.println(strServerMsg);

strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();

System.out.println("\nEnter element(Data) to INSERT IN MIDDLE in Linklist : ");
strClientMsg=br.readLine();

toServer.println(strClientMsg);
toServer.flush();
break;

case 3:
System.out.println("\nEnter element(Data) to INSERT AT LAST in Linklist : ");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
break;

case 4:
strServerMsg=frmServer.readLine();
System.out.println("\n"+strServerMsg);
break;

case 5:
strServerMsg=frmServer.readLine();
System.out.println(strServerMsg);

strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
break;

case 6:
strServerMsg=frmServer.readLine();
System.out.println("\n"+strServerMsg);
break;

case 7:
System.out.println("\nYour Linklist Contains :");
strServerMsg=frmServer.readLine();
System.out.println("\n"+strServerMsg);
break;

case 8:
s.close();
break;

default :
System.out.println("\n***  Invalid Option  ***.");
break;
}

if(option!=8)
{
System.out.println("\nDo u want to continue (y/n)");
strClientMsg=br.readLine();
toServer.println(strClientMsg);
toServer.flush();
}

}while(strClientMsg.equalsIgnoreCase("y"));
System.out.println("\n\nThank you");
}
}

Server.java   

import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(1111);
System.out.println("\nServer is Instantiated.");

int option;
String strClientMsg;
List<String> myLinkList=new ArrayList<String>();

while(true)
{
Socket s=ss.accept();
System.out.println("\nConnection Established.");

BufferedReader frmClient=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream toClient=new PrintStream(s.getOutputStream());

do
{
strClientMsg=frmClient.readLine();
option=Integer.parseInt(strClientMsg);

switch(option)
{
case 1:
strClientMsg=frmClient.readLine();
myLinkList.add(0, strClientMsg);
break;

case 2:
int index1=0;
if(!myLinkList.isEmpty())
{
toClient.println("Select any index from this range to insert data : (0 - "+(myLinkList.size()-1)+")");
toClient.flush();
strClientMsg=frmClient.readLine();
index1=Integer.parseInt(strClientMsg);
}
else
{
toClient.println("Sorry Linklist is empty : Press any key to insert element at beginning...");
toClient.flush();
strClientMsg=frmClient.readLine();
}

strClientMsg=frmClient.readLine();
myLinkList.add(index1, strClientMsg);
break;

case 3:
strClientMsg=frmClient.readLine();
myLinkList.add(myLinkList.size(), strClientMsg);
break;

case 4:
if(!myLinkList.isEmpty())
{
String strRemovedElement=myLinkList.get(0);
myLinkList.remove(0);
toClient.println("FROM BEGINNING : "+strRemovedElement+" element is removed from Linklist.");
toClient.flush();
}
else
{
toClient.println("Sorry : Linklist is Empty.");
toClient.flush();
}

break;

case 5:
int index2=0;
if(!myLinkList.isEmpty())
{
if(myLinkList.size()==1)
{
toClient.println("There is only one element in Linklist.Press any key to delete it...");
toClient.flush();
strClientMsg=frmClient.readLine();
myLinkList.remove(index2);
}
else
{
toClient.println("Select any index from this range to delete data : (0 - "+(myLinkList.size()-1)+")");
toClient.flush();
strClientMsg=frmClient.readLine();
index2=Integer.parseInt(strClientMsg);
myLinkList.remove(index2);
}
}else
{
toClient.println("Sorry Linklist is empty : Press any key to continue...");
toClient.flush();
strClientMsg=frmClient.readLine();
}

break;

case 6:
if(!myLinkList.isEmpty())
{
String strRemovedElement=myLinkList.get(myLinkList.size()-1);
myLinkList.remove(myLinkList.size()-1);
toClient.println("FROM END : "+strRemovedElement+" element is removed from Linklist.");
toClient.flush();
}
else
{
toClient.println("Sorry : Linklist is Empty.");
toClient.flush();
}

break;

case 7:
toClient.println(myLinkList);
toClient.flush();
break;

case 8:
s.close();
break;
}

strClientMsg="";
if(option!=8)
{
strClientMsg=frmClient.readLine();
}

}while(strClientMsg.equalsIgnoreCase("y"));
}
}
}

Output:





Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment