MAIN MENU BIODATA ASSIGNMENTS TUTORIALS EMAILS
DISTRIBUTED SYSTEMS (TCT 2034)
ASSIGNMENT 1
(SOCKET-PROGRAMMING)
QUESTION:
Multithreaded servers are quite popular, especially because of the increasing multiprocessing servers. Use several client applications and have each of them connect to the server simultaneously. Use a vector to store the client threads. Vector provides several methods of use in this exercise. Method size determines the number of elements in a Vector. Method elementAt returns the element in the specified location (as an Object reference). Method add places a new element at the end of the Vector. Method remove deletes it argument from the Vector. Method lastElement returns an Object reference to the last object you inserted in the Vector.
Answer:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;
public class StockServer {
private static ServerSocket servSock;
private static final int PORT = 1234;
private static Socket link;
private static Vector bookVectorOut;
private static BufferedReader istream;
private static ObjectOutputStream ostream;
public static void main(String[] args)
{
System.out.println("Opening port...\n");
try
{
servSock = new ServerSocket (PORT);
}
catch (IOException e)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
bookVectorOut = new Vector();
Stock[] staff =
{new Stock(123,"Cinderella","10"),
new Stock(234,"Killing Him Softly","20"),
new Stock(555,"Harry Porter 2","15"),
new Stock(566,"Beautiful Soul","25"),
new Stock(656,"Bioinformatics","10"){;
for(int i=0; i<book.length; i++)
bookList.add(book[i]; //Insert into Vector
for (int i=0; i<bookList.size(); i++)
{
Stock p = (Stock)bookList.elementAt(i);
System.out.println("Stock Number: "+p.getStockNum());
System.out.println("Stock Book Title: "+p.getTitle());
System.out.println("Stock Quantity: "+p.getQuantity());
}
}
}
public static void run()
{
do
{
try
{
link = servSock.accept();
istream = new BufferedReader(
new InputStreamReader(
link.getInputStream()));
ostream = new
ObjectOutputStream(link.getOutputStream());
String message = isstream.readLine();
if(message.equals("SEND STOCK DETAILS"))
{
ostream.writeObject(bookVectorOut);
ostream.close();
}
System.out.println( "\n* Closing connection...*");
link.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}while (true);
}
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an VectorList container using an Iterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
Iterator i = stockA.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an VectorList container using a ListIterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
counter = 0;
StockIterator si = stockA.stockIterator();
while (li.hasNext()) {
System.out.println("Element [" + counter + "] = " + li.next());
System.out.println(" - hasPrevious = " + li.hasPrevious());
System.out.println(" - hasNext = " + li.hasNext());
System.out.println(" - previousIndex = " + li.previousIndex());
System.out.println(" - nextIndex = " + li.nextIndex());
System.out.println();
counter++;
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Search for an object and return the first and last (highest) index. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("First occurance search for String \"Cinderella\". Index = " + stockA.indexOf("Cinderella"));
System.out.println("Last Index search for String \"Bioinformatics\". Index = " + stockA.lastIndexOf("Bioinformatics"));
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Sort the Sub-Stocklist created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Original StockList : " + stockSub);
Collections.sort(stockSub);
System.out.println("New Sorted StockList : " + stockSub);
System.out.println();
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Reverse the Sub-StockList created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Original StockList : " + stockSub);
Collections.reverse(stockSub);
System.out.println("New Reversed StockList : " + stockSub);
System.out.println();
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Check to see if the Stock Lists are empty. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Is List A empty? " + stockA.isEmpty());
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| NOTE: The contents of the Stock List are object references. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("List A (before) : " + stockA);
System.out.println();
System.out.println("List A (after) : " + stockA);
System.out.println();
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Remove (clear) Elements from List A. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("List A (before) : " + stockA);
System.out.println();
stockA.clear();
System.out.println("List A (after) : " + stockA);
System.out.println();
}
---------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.*;
public class StockClient
{
private static InetAddress host;
private static final int PORT = 1234;
public static void main(String[] args)
throws ClassNotFoundException
{
try
{
host = inetAddress.getLocalHost();
}
catch(UnknownHostException e)
{
System.out.println("Host ID not found!");
System.exit(1);
}
run();
}
private static void run() throws ClassNotFoundException
{
try
{
Socket link = new Socket(host,PORT);
ObjectInputStream istream =
new ObjectInputStream(link.getInputStream());
PrintWriter ostream =
new PrintWriter(link.getOutputStream(), true);
//Set up stream for keyboard entry...
BufferedReader userEntry =
new Bufferedreader(
new InputStreamReader(System.in));
ostream.println("SEND STOCK DETAILS");
Vector response = (Vector)istream.readObject();
System.out.println("\n* Closing connection...*");
link.close();
int bookCount=0;
for(int i=0; i<response.size(); i++)
{
Stock s=(Stock)response.elementAt(i);
bookCount++;
System.out.println(
"Stock Number:" + p.getStockNum());
System.out.println(
"Stock Book Title:" + p.getTitle());
System.out.println(
"Stock Quantity:" + p.getQuantity());
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
---------------------------------------------------------------------------------
class Personnel
//As defined in earlier example, but without implementation
//Serializable interface
{
private long stockNumber;
private String title;
private String stockQty;
public Stock(long stockNum, String title, String stockQty)
{
stockNumber = StockNum;
title = tBook;
stockQty = sQty;
}
public long getStockNum()
{
return stockNumber;
}
public String getTitle()
{
return title;
}
public String getQuantity()
{
return stockQty;
}
public void setTitle(String tBook)
{
title = tBook;
}
--------------------------------------------------------------------------------------------------------------------------------------
OUTPUT