Temperature Server


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Temperature Server

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    55

    Temperature Server

    I am creating a temperature server which allows the client to ask for a temperature via typing a city. I am using a hashmap. I need help in getting the server to display the temperature. I am currently using an OutputStream but i think i need to use an ObjectStream, any ideas? Below is the current code for the temperatureClient and temperatureServer.

    Temperature client

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class TemperatureClient {
    public static final int PORT = 1234;


    /**
    * @param args
    */
    public static void main(String[] args) {
    InetAddress HOST;
    try {
    HOST = InetAddress.getLocalHost();
    Socket client = new Socket(HOST, PORT);
    BufferedReader serverMsg = new BufferedReader(new InputStreamReader(
    client.getInputStream()));
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter output = new PrintWriter(client.getOutputStream(),
    true);
    String currentMsg = "";
    System.out.println("Ask for a temperature in a particular server, type 'quit' to exit the program");
    while(true) {
    System.out.print(">> ");
    currentMsg = userInput.readLine();
    output.println(currentMsg);
    if(currentMsg.equalsIgnoreCase("quit")) {
    System.out.println("Exiting temperature client..");
    serverMsg.close();
    output.close();
    userInput.close();
    client.close();
    return;
    }
    System.out.println("Temperature request sent to server...");
    System.out.println("Temperature in " + userInput.readLine()+ " is " + serverMsg.readLine());
    }

    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    }


    Temperature server

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.HashMap;
    import java.util.StringTokenizer;


    public class TemperatureServer implements Runnable {
    public static final int PORT = 1234;
    HashMap temperatureMap = new HashMap();

    public Socket client;

    public TemperatureServer() {
    try {
    ServerSocket theServer = new ServerSocket(PORT);
    while (true) {
    client = theServer.accept();
    new Thread(this).start();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void getCityTemperatures(){
    temperatureMap.put("Melb", new Double(18.9));
    temperatureMap.put("Syd", new Double(6.00));
    }

    public void run() {
    BufferedReader input;
    PrintWriter output;
    String line = new String();

    try {
    input = new BufferedReader(new InputStreamReader(client
    .getInputStream()));
    line = input.readLine();
    output = new PrintWriter(client.getOutputStream(), true);
    while (processMessage(input, output))
    ;
    input.close();
    output.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    if(temperatureMap.isEmpty()){
    System.out.println("\nCity not on file\n");
    return;
    }

    StringTokenizer cityTemperature = new StringTokenizer(line);
    if (cityTemperature.countTokens()!=1) {
    System.out.println("\nError encountered :: Not enough information entered\n");
    return;
    }

    String cregisteration = cityTemperature.nextToken();

    String key = cityTemperature.nextToken();
    temperatureMap.get(key);
    }


    // private synchronized boolean processMessage(BufferedReader
    // input,PrintWriter output) {
    private boolean processMessage(BufferedReader input, PrintWriter output) {
    try {
    String str = input.readLine();
    if (str.equalsIgnoreCase("quit"))
    return false;
    System.out.println("The temperature for the city the user entered is: " + str);

    } catch (IOException e) {
    e.printStackTrace();
    return false;
    }
    return true;
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    new TemperatureServer();
    }


    }

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    What you send back to the client depends on how you design it. You probably could go either way. If the data is somewhat complicated and needs what an Object would provide to handle it on the client, then return an object.
    If the data is simple enough, just send it back in a stream.

    I need help in getting the server to display the temperature.
    How/where are you trying to display on the server? Or do you mean the client?
    println() is the simplest. Or use a dialog box. Or have a textarea that you append the output to.

    If you are having problems and getting error messages, copy and paste them here.

    A few comments in your code would help someone reading it understand what you WANT the code to do.

Similar Threads

  1. find version & service packs
    By rperez in forum Database
    Replies: 5
    Last Post: 01-02-2009, 05:14 PM
  2. query tuning
    By dhaya in forum Database
    Replies: 11
    Last Post: 08-25-2003, 06:24 PM
  3. Replies: 4
    Last Post: 12-10-2000, 04:15 PM
  4. Replies: 7
    Last Post: 09-15-2000, 09:44 AM
  5. SQL server administrative knowledge
    By Becky in forum Database
    Replies: 1
    Last Post: 07-11-2000, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center