Link to home
Start Free TrialLog in
Avatar of jaycangel
jaycangel

asked on

Hashtable problem when working with Threads and Sockets

I'm a newbit at Java, i'm having a problem with creating an employee instance. When the instance is created a hashtable is set up with with some staff information. Problem is that this information seems to be lost somewhere?!

in Client the following line getNoEmployees should write out 2, but instead i get an exception null error. Its like the employeeTable varible has been lost?! Why is this - does it have something to do with threads?

        employeeTable = new Employee();
        System.out.println(employeeTable);
        employeeTable.getNoEmployees();


I've included all my code bellow. It simply sets up a socket to wait for input from a client, that will return employee information.

Thank you for all you help,
Jason

------
import java.net.*;
import java.io.*;
import java.util.*;


/**
 * Contains employee information
 */
class Employee
{
  public Hashtable employeeTable;

  /**
   * Construct employee hashtable
   */
  public Employee()
  {
    Hashtable employeeTable = new Hashtable();
    employeeTable.put("a", "10000");
    employeeTable.put("b", "20000");
    employeeTable.put("c", "30000");
    System.out.println("Employee Table set up");
    System.out.println(employeeTable.get("a"));
  }


  public int getNoEmployees()
  {
    return employeeTable.size();
  }

  public String getSalaryOf(String i_employeeName)
  {
    return (String) employeeTable.get(i_employeeName);
  }

  public int getNoEmployeesWithSalaryGreaterThan(int i_salary)
  {
    return 1;
  }
}

public class Server extends Thread {
    private ServerSocket serverSocket;
    public  Server serverInstance;

    /**
     * Constructor to create Server
     */
    public Server() {
        try { serverSocket = new ServerSocket(2048); }
        catch (IOException e) { writeCatch(e, "Error creating server socket"); }
        System.out.println("TMA01Q2 server on port 2048");
        this.start(); // run
    }

    /**
     * Listen and respond to messages
     */
    public void run()
    {
      System.out.println("Server running");
      try
      {
        while(true)
        {
          System.out.println("Connections being accepted");
          Socket cSocket = serverSocket.accept (); // accept connection
          CSpawn c = new CSpawn(cSocket); // when connection recieved spawn new connection thread
        }
      }
      catch (IOException exp)
      {
        writeCatch(exp, "Error waiting for connection");
      }
    }

    public void isRunning()
    {
      System.out.println("is running");
    }

    /**
     * Start server
     */
    public static void main(String[] args)
    {
        new Server();
    }

    /**
     * Write out exception along with message for debugging
     */
    public static void writeCatch(Exception iExp, String iMsg)
    {
      System.out.println(iMsg + " => " +  iExp);
      Runtime.getRuntime().exit(666);
    }
}

/**
 * class handles socket threads made by client
 */
class CSpawn extends Thread
{
    private DataInputStream in;
    private PrintStream out;
    private Socket client;
    private Employee employeeTable;

    /**
     * Constructor to start connection
     */
    public CSpawn(Socket cSocket)
    {
        client = cSocket;
        try
        {
            in = new DataInputStream(client.getInputStream());
            out = new PrintStream(client.getOutputStream ());
        }
        catch (IOException exp)
        {
            try
            {
              client.close();
            }
            catch (IOException exp2)
            {
              // do nothing
            }
            return;
        }
        this.start(); // run
    }

    /**
     * Read and Analyse Input
     */
    public void run()
    {
        employeeTable = new Employee();
        System.out.println(employeeTable);
        employeeTable.getNoEmployees();

        String inputLine;
        try
        {
            while(true)
            {
                // read line from steam
                inputLine = in.readLine();
                out.println("10000");
            }
        }
        catch (IOException exp)
        {
          // ignore
        }
        finally
        {
          try
            {
              client.close();
            }
            catch (IOException exp)
            {
              //ignore
            }
        }
        // exceptions ignored because they are most likely caused by client closing
        // important exceptions are in server class
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The local var shadowes the member var
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avinthm
avinthm

i m late :(
Avatar of jaycangel

ASKER

Damn you are good! I've been struggling for ages!!!! And it takes you like 15 second!

Maybe you could help me with this too.

The CSpawn class contains the instance of employee. Which means that everytime someone connects to the server an new employee instance is created along with the CSpawn (connection thread). How would I change the code so that there was only one employee instance located in the server class. I just don't know how the CSpawn class would refer back to the Server class that created it. Is there something like parent.employee ?!
create one in your Server class and pass  it as a parameter to the Cspawn constructor
Thanks, that works perfectly
no worries :)