Showing posts with label DC. Show all posts
Showing posts with label DC. Show all posts

Monday, June 14, 2010

b. Implementation of queue

-------------------------------------------------------------------------------------------------------------
SOURCE CODE
----------------------------------------------------------------------------------------------


Server Program :

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

//class implementing Queue operations
class Queue{

private int queue[];
private int toq;
private int eoq;

// allocate and initialize queue
Queue(int size){
queue = new int[size];
toq = 0;
eoq = -1;
}
//check whether queue is full
boolean isFull(){
if(eoq == queue.length-1)
return true;
else
return false;
}
//check whether queue is empty
boolean isEmpty(){
if(eoq < 0)
return true;
else
return false;
}

// Push an item into queue
void push(int item){
for(int i=eoq;i>-1;i--){
queue[i+1]=queue[i];
}
++eoq;
queue[0] = item;
}

// Pop an item from the queue
int pop(){
return queue[eoq--];
}
//get all elements of queue
int[] getQueue(){
return queue;
}
//get current size of queue
int getCurrentSize(){
return (eoq+1);
}
}

//class implementing Queue Server operations
class QueueServer implements Runnable{

static ServerSocket serverSocket;
Socket socket;
BufferedReader read,from;
PrintWriter to;
Thread thread;
Queue queue;
int clientID;
static int clientCounter=0;

//constructor
QueueServer(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 queue: ");
int size=Integer.parseInt((from.readLine()).trim());
queue=new Queue(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(queue.isFull())
to.println("Queue Full. What next??");
else{
to.println("Enter element: ");
item=Integer.parseInt((from.readLine()).trim());
queue.push(item);
to.println("Pushed item. What next??");
}
}
else if(action.equals("pop")){
if(queue.isEmpty())
to.println("Stack Empty. What next??");
else
to.println("Popped item: "+queue.pop()+" What next??");
}
else if(action.equals("display")){
if(queue.isEmpty())
to.println("Queue Empty. What next??");
else{
int[] q=queue.getQueue();
String queueStr="";
for(int i=0;i queueStr=queueStr+" "+q[i];
to.println("Queue contains: "+queueStr+" 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();
QueueServer server=new QueueServer(s);
}
}
}


Client Program :

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

//class implementing client operations
class QueueClient{

//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 of client
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);}
}
}

Simulating Network Operating System (NOS) commands

Simulating Network Operating System (NOS) commands
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 stckStr=stckStr+" "+stck[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);}
}
}

Saturday, January 16, 2010

Simulating Network Operating System commands

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();
}
}