Lab10 Solution 1 - Acronym Server using sockets

Lab 10 Exercise 1 solution - IT Acronym Server using sockets and object serialization

This is the multi threaded version of the server

The server

import java.io.*;
import java.net.*;
import java.util.Map;
import java.util.HashMap;
 
public class ITAcronymServer {
    public static void main(String args[]) throws IOException {
        Socket client = null;
        ServerSocket server = new ServerSocket(8000);
        initAcronymMap();
        while (true) {
            client = server.accept();
            //Pass to the request handler thread
            Thread acroServerThread = new AcronymServerThread(client, acronymMap);
            acroServerThread.start();
        }
    }
 
    private static void initAcronymMap() {
        acronymMap = new HashMap();
        acronymMap.put("JVM", "Java Virtual Machine");
        acronymMap.put("RMI", "Remote Method Invocation");
        acronymMap.put("CAS", "Compare And Swap");
        acronymMap.put("TCP", "Transmission Control Protocol");
    }
 
    private static Map acronymMap = null;
}

The request handler thread

import java.io.*;
import java.net.*;
import java.util.*;
 
public class AcronymServerThread extends Thread {
    private Socket client = null;
    private Map acronymMap = null;
 
    public AcronymServerThread(Socket _client, Map _acronymMap) {
        client = _client;
        acronymMap = _acronymMap;
    }
 
    public void run() {
        try {
            //get the request from the client
            ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
            Object requestObject = ois.readObject();
            //cast the request object into a string 
            String requestAcronym = (String) requestObject;
 
            String fullForm = (String) acronymMap.get(requestAcronym);
 
            if (fullForm == null)
                fullForm = requestAcronym + " not found";
 
            //write the fullForm back to the client
            ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeObject(fullForm);
 
        } catch (IOException ioex) {
                System.out.println("net stream error" + ioex.toString());
        } catch (Exception ex) {
                System.out.println("Exception:" +ex.toString());
        }
 
    }
}

The client

import java.io.*;
import java.net.*;
 
public class AcronymClient {
    public static void main(String args[]) {
        try {
            //create a connection to the server
            Socket socket = new Socket("localhost", 8000);
 
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(args[0]);
 
            //get the result
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String result = (String) ois.readObject();
 
            System.out.println(result);
 
            socket.close();
        } catch (IOException ioex ) {
                ioex.printStackTrace();
        } catch (Exception ex) {
                ex.printStackTrace();
        }
 
    }
}