Lab 8 - Distributed Programming

Distributed Programming Lab

Sample code from lecture slides

Exercises

1. Write a server called PrimeSortServer using sockets which can recieve an ArrayList of integers from a client and converts that ArrayList into another ArrayList which include only numbers which are prime. This list of prime numbers is sent back to the client as response.

2. Write the client to test the PrimeSortServer called PrimeSortClient

3. Multithread the following Currency Rate Server and also write a client for it

import java.io.*;
import java.util.*;
import java.net.*;
 
public class CurrencyRateServer {
    public static void main(String args[]) throws Exception {
        initRatesMap();
 
        ServerSocket serverSocket = new ServerSocket(7777);
        Socket clientSocket;
 
        while(true) {
            clientSocket = serverSocket.accept();
 
            ObjectInputStream ois = new ObjectInputStream(
                                            clientSocket.getInputStream());
            Object inObject = ois.readObject();
            //convert to string 
            String otherCurrencyCode = (String) inObject;
 
            //get the rate from the ratesMap
            Object rate = ratesMap.get(otherCurrencyCode);
 
            ObjectOutputStream oos = new ObjectOutputStream(
                                            clientSocket.getOutputStream());
            //write the rate to the client
            oos.writeObject(rate);
 
            clientSocket.close();
        }
    }
 
    private static void initRatesMap() {
        ratesMap.put("USD", new Double(3.0));
        ratesMap.put("GBP", new Double(1.5));
        ratesMap.put("AUD", new Double(3.0));        
    }
 
    private static Map ratesMap = new HashMap();
}