?
Solved

calculation  delay!!!

Posted on 2003-03-15
16
Medium Priority
?
196 Views
Last Modified: 2010-03-31
hi i have a client-server application.
Idea:
in the server side i have a GUI to store objects into the Hashtable.
the client sends some key values to the server(using B1 button) and the server checks into the hashtable (matching or not) and send back a message saying that the Object variables are available or not.(no problem all working!)
the problem occures when the client sends an int from the textfield to the server(using B2 button).
in the server program we have a class (Calculation) that uses the recived value (from the client) and multiply it with a an object variable(from another class object) from the Hashtable to get the result and send it back to the client.

it is all working but after the second press on the B2 button(in the client side)!!!.
why is that???

NOTE:
i'm using a stream socket for connection.
using PrintWriters to send data
and BufferedReaders to recive data

thanks...
0
Comment
Question by:salim_Qatan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143058
Not enough information here i'm afraid.
0
 

Author Comment

by:salim_Qatan
ID: 8143183
the code is too ling though ...
but i'll try to post the parts that concerns..

starting with the client:
in the try bock i have a while loop:
while (true) {
         line = br.readLine();
         if ( line.equals ( "displayObject" ) )/*the first line sent from the server when there is a match with the sent Keys*/
 {
// do stuff (display object Variables sent from the server)
         
         }
         else if  ( line.equals ( "totalPrice" ) ){ //this line displays the calculation result.
         line1 = br.readLine();
         tf10.setText(line1);
         }
         }

--------------
// actionPerformed (client)
public void actionPerformed(ActionEvent e) {
       if (e.getSource() == B1){
           ps.println("submitButton");//first line (used for if condition)
           ps.println(key1 + key2 + key3); // sending key values to the server(no problem!)
         }

if (e.getSource() == B2){
          int reqQuan;
          reqQuan = Integer.parseInt(tf9.getText());
          ps.println("enterButton");//first line (used for if condition in the while loop on the other side)
          ps.println(reqQuan); //sending textfield value
}
}

**************************
here is the server side:
in the try block:
while ( true )
{
 line = br.readLine () ;

 if ( line.equals ( "submitButton" ) ) {
      line1 =  br.readLine ();
     // to check if the incomming line from the client = the key in the Hashtable
     if (myTable.containsKey(line1) )
     {
        ps.println("displayObject");//sending first line
       //do stuff sending object variable(all working no problem!)
         }
       }
//here is the problem!!!
     if ( line.equals ( "enterButton" ) ) {
      CarComponent component = (CarComponent)myTable.get(str1+str2+int1);//to be able to get object variables from the hashtable to use them in Calculation class
       myCalculation = new Calculation(reQuan,origPrice,avQuantity); // Create Calculation object
       //origPrice , reQuan and avQuantity are declared in the main class as float,int and int respectively
        origPrice  = component.price ;
        avQuantity =  component.availableQuantity;
        line1 = br.readLine();
        reQuan = Integer.parseInt(line1);

       ps.println("totalPrice"); a line sent to be used in the while loop if statement
       ps.println(myCalculation.calacTotalPrice(totalPrice)); //to send the calculated value
    }
  }
------------------------
// in the actionPerform() we just store object into the Hashtable (all working no problem)
------------------------
//here is the CarComponent class
class CarComponent{

   //definig variables of the class
   public String modelName, partName;
   public int builtYear, availableQuantity;
   public float price;

   // a method to create an object which contains the entered values from the textfields
   public CarComponent(String modelField, String partField
   , int builtYearField, float priceField, int quantityField)
   {
      modelName = modelField;
      partName  = partField;
      builtYear = builtYearField;
      price     = priceField ;
      availableQuantity = quantityField;
   }
}
//calculation class
class Calculation{

   //definig variables of the class
   public float origenalPrice;
   public int requiredQuantity,origenalQuantity;

   //the variables reQuan and origPrice are from the CarComponent Object
   public Calculation (int reQuan,float origPrice, int avQuantity) {
    requiredQuantity =  reQuan;
    origenalPrice =  origPrice;
    origenalQuantity = avQuantity;
  }
   public float calacTotalPrice(float totalPrice){
   totalPrice = requiredQuantity*origenalPrice;
   return totalPrice;
  }
}

hope that helps...

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143211
OK. Please clarify this - what is happening (or not)?

>>
it is all working but after the second press on the B2 button(in the client side)!!!.
why is that???
>>
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:salim_Qatan
ID: 8143269
sorry about that :|
here it is ...
in the client program:
after getting the object values (from CarComponent class)displayed, the client enters a number into a textfield (tf9)and press the B2 button to get the calculated result displayed in the textfield(tf10).
when the button B2 first pressed the displayed value is 0.0 after the second press the correct calculated value appears!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143300
I must confess I'm having difficulty in understanding the application flow (communication sequence) beginning with the initial click of B2. Can you clarify the steps?
0
 

Author Comment

by:salim_Qatan
ID: 8143427
1.running the server program.
2.running the client program (connected together successfully)
3.using the server GUI to store an object into the Hashtable (using 5 Txetfields and a button , the first 3 textfields are used to enter the key values)
4.in client GUI we have 10 textfields and two buttons

like this :
****************************************************
--------tf1          --------tf2         --------tf3
       
                              =B1=
--------tf4
--------tf5
--------tf6
--------tf7
--------tf8

***************************************************

----------tf9            =B2=

---------tf10

***************************************************

5.the first 3 textfields used to enter the key values (Hashtable key values) by pressing B1 to get the object variables from the server program if those sent keys matches the one stored it will return 5 object variables(component.ModelName, component.partName  , component. builtYear, component.price, component.availableQuantity )to be displayed in tf4,tf5,tf6,tf7 and tf8 respectively.
6.then the client will choose a required quantity number by entering it in tf9 and pressing the button B2
to get the calculated total price .

that’s all!.
The problem is when the client enters a number (int) into tf9 and presses the button B2 he should be getting the total price immediately from the first button(B2) press.
Whereas in our case he gets 0.0 by the first press and by the second press he gets the correct total price!!!
Why is that ?!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143512
I'm not sure yet, but you can start by altering the following code, as it won't do any harm:

>>
if (e.getSource() == B2){
         int reqQuan;
         reqQuan = Integer.parseInt(tf9.getText());
         ps.println("enterButton");//first line (used for if condition in the while loop on the other side)
         ps.println(reqQuan); //sending textfield value
}
>>

Wrap this in a try catch block

reqQuan = Integer.parseInt(tf9.getText());

and only write to the socket if it's valid and not zero [unless zero *is* a valid value]

add a ps.flush() just in case

Let me know what happens
0
 

Author Comment

by:salim_Qatan
ID: 8143572
i appritiate your help, but i think that its not the problem.
the problem is in the server program:
>>

//here is the problem!!!
    if ( line.equals ( "enterButton" ) ) {
     CarComponent component = (CarComponent)myTable.get(str1+str2+int1);//to be able to get object variables from the hashtable to use them in Calculation class
      myCalculation = new Calculation(reQuan,origPrice,avQuantity); // Create Calculation object
      //origPrice , reQuan and avQuantity are declared in the main class as float,int and int respectively
       origPrice  = component.price ;
       avQuantity =  component.availableQuantity;
       line1 = br.readLine();
       reQuan = Integer.parseInt(line1);

      ps.println("totalPrice"); a line sent to be used in the while loop if statement
      ps.println(myCalculation.calacTotalPrice(totalPrice)); //to send the calculated value
   }
 }
>>

for example if we change >>ps.println(myCalculation.calacTotalPrice(totalPrice));
with ps.println(component.price );
we will get that object variable immediately from the first press on B2!!!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143603
OK. Is

>>myCalculation.calacTotalPrice(totalPrice));

accessing the gui? What's actually happening here?
0
 

Author Comment

by:salim_Qatan
ID: 8143689
I think it would be much clearer if I post the whole server code here:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class MyServer extends Frame implements ActionListener{

    //definig variables
    private TextField modelField,partField,builtYearField
    ,priceField,quantityField;
    private Button submit;    
    private Panel p1,p2,p3,p4,p5,p6,p7;
    private Hashtable myTable;
    private Calculation myCalculation;
    private CarComponent component;
    String line1 = "", line = "";  
    String str1,str2;
    int int1,int2,reqQuan,reQuan,avQuantity;
    float floatPrice,origPrice,totalPrice;
    //connection stuffs
    private Socket s=null;
    private ServerSocket ss=null;
    private BufferedReader br = null;
    private PrintWriter ps = null;

        public MyServer() {

           //setting up the layout and building the GUI with 5 textfields and a button          
          // creating the Hashtable
          myTable = new Hashtable();
          setSize(new Dimension(370, 244));        
          show();          
          addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent e){
            try{
              System.exit(0);
              ps.close();
              br.close();
              s.close();
              ss.close();
            }
            catch(IOException iE){
            }
        }
   });

//try block
   try{
     ss = new ServerSocket(2000);
     s = ss.accept();
     ps = new PrintWriter(s.getOutputStream(), true);
     br = new BufferedReader(new InputStreamReader(s.getInputStream()));
     // the while loop
     while ( true )
{
 line = br.readLine () ;

 if ( line.equals ( "submitButton" ) ) {
      line1 =  br.readLine ();
     // to check if the incomming line from the client = the key in the Hashtable
     if (myTable.containsKey(line1) )
     {
       /* if its true, it will use Hashtable method get()
       to retrive object variables from the Hashtable to send it back to Client */
       CarComponent component = (CarComponent)myTable.get(line1);
       //sending back the Object variables to the client
       ps.println("displayObject");
       ps.println(component.modelName);
       ps.println(component.partName);
       ps.println(component.builtYear);
       ps.println(component.price);
       ps.println(component.availableQuantity);

      }
   
       }
     if ( line.equals ( "enterButton" ) ) {
      CarComponent component = (CarComponent)myTable.get(str1+str2+int1);//to be able to get object variables from the hashtable
       myCalculation = new Calculation(reQuan,origPrice,avQuantity); // Create Calculation object
        origPrice  = component.price ;
        avQuantity =  component.availableQuantity;
        line1 = br.readLine();
        reQuan = Integer.parseInt(line1);
       ps.println("totalPrice");
       ps.println(myCalculation.calacTotalPrice(totalPrice)); // the problem!!!
    }
       }
 }
      catch(IOException iExc){ }
 }
  public static void main(String[] args) {
      MyServer server = new MyServer();
      }
 
  public void actionPerformed(ActionEvent e) {
           if (e.getSource() == submit){

          // defining the variables that be used
          str1 = modelField.getText();
          str2 = partField.getText();
          int1 = Integer.parseInt(builtYearField.getText());
          int2 = Integer.parseInt(quantityField.getText());
          Float float1 = Float.valueOf(priceField.getText());
          floatPrice = float1.floatValue();
               CarComponent component = new CarComponent(str1,str2,int1,floatPrice,int2);            
               Object val = myTable.put(str1+str2+int1,component);
      }
  }
}

//creating a class called carComponent
class CarComponent{
   //definig variables of the class
   public String modelName, partName;
   public int builtYear, availableQuantity;
   public float price;
   public CarComponent(String modelField, String partField
   , int builtYearField, float priceField, int quantityField)
   {
      modelName = modelField;
      partName  = partField;
      builtYear = builtYearField;
      price     = priceField ;
      availableQuantity = quantityField;
   }
}
class Calculation{
   //definig variables of the class
   public float origenalPrice;
   public int requiredQuantity,origenalQuantity;
   public Calculation (int reQuan,float origPrice, int avQuantity) {
    requiredQuantity =  reQuan;
    origenalPrice =  origPrice;
    origenalQuantity = avQuantity;
  }
   public float calacTotalPrice(float totalPrice){
   totalPrice = requiredQuantity*origenalPrice;
   return totalPrice;
  }
}


0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 8143798
I'm tempted to think that this could be being caused by the interaction between the event thread and your client/server threads, but i'm not sure how at the moment.
0
 

Author Comment

by:salim_Qatan
ID: 8143839
hi thanks for thinking with me
i've sorted it out, its working now :o)
anyhow you do deserve those points.
thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8143857
>>i've sorted it out, its working now :o)

Please tell me how and why!
0
 

Author Comment

by:salim_Qatan
ID: 8144000
ok
the idea was to get the initial price and the requiredQuantity multiply them together to get the totla price.
in the client program i've done something like this:
//
if (e.getSource() == enter){
          int reqQuan;
          float recivedPrice;
          Float value = Float.valueOf(tf7.getText());
          recivedPrice = value.floatValue();
          reqQuan = Integer.parseInt(tf9.getText());
          ps.println("enterButton");
          ps.println(reqQuan);
          ps.println(recivedPrice);  // float Price line from the textfield tf7
          ps.println(Integer.parseInt(tf8.getText()));   //int availableQuantity line from tf8
       }

//
actualy the tf4,tf5,tf6,tf7 and tf8 are not editable!!

in the server program:
i've replaced this:
>>

    if ( line.equals ( "enterButton" ) ) {
     CarComponent component = (CarComponent)myTable.get(str1+str2+int1);//to be able to get object variables from the hashtable
      myCalculation = new Calculation(reQuan,origPrice,avQuantity); // Create Calculation object
       origPrice  = component.price ;
       avQuantity =  component.availableQuantity;
       line1 = br.readLine();
       reQuan = Integer.parseInt(line1);
      ps.println("totalPrice");
      ps.println(myCalculation.calacTotalPrice(totalPrice)); // the problem!!!
   }
>>

with this:
if ( line.equals ( "enterButton" ) ) {
         line1 = br.readLine();// requiredQuantity line
         line2 = br.readLine(); //price line
         line3 = br.readLine(); //availableQuantity line
         reQuan = Integer.parseInt(line1);
         avQuantity = Integer.parseInt(line3);
         Float value = Float.valueOf(line2);
         origPrice = value.floatValue();
         myCalculation = new Calculation(reQuan,origPrice,avQuantity); // Create Calculation object

         ps.println("totalPrice");
         ps.println(myCalculation.calacTotalPrice(totalPrice));
    }

//
i can see that this one is much better than the previous one becouse in the previous one when you press B2 button you will get the total price of the last object stored into the hashtable, whereas in this one you only get the total price of the object that you asked for.

about the delay with this one no delay!!!
even nothing else changed .
i hope that right.
it should be.
cheers.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8144022
Hmm. Still don't know what the problem was, but never mind!

Incidentally, you can shorten

>>

        Float value = Float.valueOf(line2);
        origPrice = value.floatValue();
>>

to

>> 
        origPrice = Float.parseFloat(line2);
>>
0
 

Author Comment

by:salim_Qatan
ID: 8144063
never mind, as long as its working ;o)
BTW thanks for that comment.
thanks anyways.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question