Saturday, January 16, 2010

IMPLEMENTATION OF QUEUE

/*Server Program */

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qser
{
//Declaration of variables
ServerSocket ss=null;
String queue[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on queue

public void check()
{
for(i=0;i<=4;i++)
{ queue[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 insert operation on queue
{
System.out.println("Insert");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(queue[i]=="")
break;
}

if(queue[4]!="")
System.out.println("Queue full...");
else
{
System.out.println("Insert Operation Performed");
queue[i]=str;
}
}

if(x==2)//For the delete operation on queue
{
System.out.println("Delete");
for(i=1;i>=5;i--)
{
if(queue[i]!="")
break;
}
System.out.println("Delete Operation Performed -" +queue[i]);
queue[i]="";
}

if(x==3) //returns the values to the client
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{

pw.println(queue[i]);
pw.flush();
}
}

if(x==4)
{
System.exit(0);
//terminates the program
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

class QueueServer //class with main method
{
public static void main(String args[])
{
qser qs=new qser();
qs.check();
}
}



/* Client Program*/

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qclient
{
//Declaration of variables
Socket s=null;
String queue[]=new String[5];
String st;
String str;
int i,x;

public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
queue[i]="";
}
try{
//establish connection with server
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}

public void Contents() throws IOException
{
System.out.println("1 Insert");
System.out.println("2 Delete");
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)
{
Insert(); // insert operation
}
if(x==2)
{
Values(); // delete operation
}
if(x==3)
{
Display();// displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}

public void Insert() throws IOException
{
System.out.println("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 queue");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
queue[i]=br2.readLine();
System.out.println(queue[i]);
}
Values();
}
}
class QueueClient
{
public static void main(String args[]) throws IOException
{
qclient cl=new qclient();
cl.check();
cl.Contents();
}
}

No comments:

Post a Comment