Link to home
Start Free TrialLog in
Avatar of ZURINET
ZURINET

asked on

While error

Hi all
Below is a method part of a Web server

The method should respond to message based on while and if condition

The problem is that if I send string EXIT  through a JTextField to the method
The first click it responds with "404 Error: Page is missing"
By the second click it responds with the correct message myResponse.

By the third  click it responds with the correct message myResponse.
And so on........

I am wondering if this problem lines in this method below

Thanks in Advance
private void answerMeMyClientReq() throws IOException
   { 
    while (!(request.length()==0))       
      {
         System.out.println("I am listining : " + request);
         if (request.equals("GET /invoice"))
         {
            toClient.println(mSample);
         }        
         
         if (request.equals("EXIT"))
         {
            toClient.println(myResponse);
         }
         
         else
         {
            toClient.println("404 Error: Page is missing");
         }      
         
         request = fromClient.readLine(); 
          
      }  
      }

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

A better pattern would be the following. try it and let me know what happens:
while ((request = fromClient.readLine()) != null && request.length() > 0)       
{
         System.out.println("I am listining : " + request);
         if (request.equals("GET /invoice"))
         {
            toClient.println(mSample);
         }        
         
         if (request.equals("EXIT"))
         {
            toClient.println(myResponse);
         }
         
         else
         {
            toClient.println("404 Error: Page is missing");
         }                                  
}  

Open in new window

Avatar of ZURINET
ZURINET

ASKER

Tried it ..
The first request was successful
The second try freezes the JTextField

Note I declared request as String

Thanks
You need to run that in a separate thread and then update the text field on the event dispatch thread. Use a SwingWorker
Avatar of ZURINET

ASKER

Okay let me give a try..
Avatar of ZURINET

ASKER

Just a technical Question.

What can cause the method above to
Return "404 Error: Page is missing" by first  click.?
Even though that EXIT is the argument supplied

knowing that we have an ActionListener
and we have
new Thread(new ConnectionThread(socket));

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of ZURINET

ASKER

Just found something interesting.

When the client starts
It sends a message to the Server
sendId () which is a string containing client information.

Hence, I decide to print out what ever first information that is send
to the server through the JTextField

And I got a suprise.. it still resend the server the initial sendId informaiton..

How can I clear this sendId form memory.?
So that what ever I typed  in the JTextField  will be delivered.
 private void connectToMyServer() 
    
   { 
      try
      {
         socket = new Socket(SERVER_ADD, SERVER_PORT);
         openStreams();
          sendId(); // Which is a String  "Clients ID"
      }
      catch (IOException e)
      {
         System.out.println("Some thing is wrong with this Server " + e);
      }
   }
   

Open in new window

Really not enough info to answer that ;-)