AIM: Simulating Network Operating System (NOS) commands
Concept: In this Practical make use of socket
programming. On the client side design an
application that w ill 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.
c) Implementation of link list
Commands: INSERT an element at the beginning, middle and end
of the linked
list, DELETE an element from the beginning, middle and end of
the linked list.
DISPLAY the linked list. EXIT.
Client program common for all.
Clnt.java
import java.io.*;
import java.net.*;
class clnt
{
public static void main(String args[])
{
try
{
String inc="";
BufferedReader br1=new BufferedReader(new
InputStreamReader(System.in));
Socket s=new Socket("localhost",9999);
BufferedReader br2=new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
System.out.println(br2.readLine());
inc=br1.readLine();
pw.println(inc);
if(inc.equals("D"))
{
s.close();
System.out.println("operation complete...");
break;
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
a. Implementation of stack.
Stack1.java
class Stack1
{
private int stack[];
private int tos;
Stack1(int size)
{
stack=new int[size];
tos=-1;
}
boolean isFull()
{
if(tos==stack.length-1)
return true;
else
return false;
}
boolean isEmpty()
{
if(tos<0)
return true;
else
return false;
}
void push(int item)
{
if(!(tos==stack.length-1))
stack[++tos]=item;
}
int pop()
{
return stack[tos--];
}
int[] getStack()
{
return stack;
}
int getCrSize()
{
return (tos+1);
}
}
srvr_stack.java
import java.io.*;
import java.net.*;
Parallel processing &
distributed computing M.Sc. IT part-ll 3
class srvr_stack implements Runnable
{
static ServerSocket ss;
Socket s;
BufferedReader br;
PrintWriter pw;
Stack1 stack;
Thread th;
int cid,size=0,sizen=0,i=0,e=0,tmpsize=0;
static int c=0;
String ch=" ";
public srvr_stack(Socket skt) throws Exception
{
this.s=skt;
cid=++c;
System.out.println("client:"+cid+"
Accepted-");
br=new BufferedReader(new
InputStreamReader(skt.getInputStream()));
pw=new PrintWriter(skt.getOutputStream(),true);
th=new Thread(this);
th.start();
}
public void run()
{
try
{
getSize();
menu();
while(!(ch.equals("D"))){
ch=br.readLine();
if(ch.equals("A")) pushdata();
if(ch.equals("B")) popdata();
if(ch.equals("C")) displaydata();
if(ch.equals("D")) exitstack();
if(ch.equals("M")) menu();
}
}
catch(Exception e)
{
pw.println(e);
}
}
public void pushdata() throws Exception
{
if(stack.isFull()) pw.println("STACK FULL..(press M for
menu)");
else
{
for(i=0;i<size;i++)
{
pw.println("ENTER ELEMENT:");
e=Integer.parseInt((br.readLine()).trim());
Parallel processing &
distributed computing M.Sc. IT part-ll 4
stack.push(e);
}
size=sizen+size;
pw.println(i+" elements pushed..(press M for
menu)");
}
}
public void popdata() throws Exception
{
if(stack.isEmpty()){pw.println("STACK EMPTY..(press M for
menu)");}
else pw.println(stack.pop()+" popped.. (press M for
menu)");
sizen=stack.getCrSize();
size=tmpsize-sizen;
}
public void displaydata() throws Exception
{
if(stack.isEmpty()){pw.println("STACK EMPTY..(press M for
menu)");}
else
{
int[] st=stack.getStack();
String str="";
for(int i=0;i<stack.getCrSize();i++)
str=str+" "+st[i];
pw.println("Stack contains: "+str+" (press M
for menu)");
}
}
public void exitstack() throws Exception
{
s.close();
System.out.println("Client "+cid+"
exited..");
}
public void menu()
{
pw.println("A.push B.pop C.display D.exit Enter
choice:");
}
public void getSize() throws Exception
{
pw.println("enter size of stack:");
size=Integer.parseInt((br.readLine()).trim());
stack=new Stack1(size);
tmpsize=size;
}
public static void main(String args[]) throws Exception
{
ss=new ServerSocket(9999);
System.out.println("server started...");
while(true)
{
Socket sckt=ss.accept();
srvr_stack server=new srvr_stack(sckt);
}}}
No comments:
Post a Comment