Link to home
Start Free TrialLog in
Avatar of ikhmer
ikhmer

asked on

process of loop doesn't finished!

Hello Experts,

I come up with another question! below is a part of my code. the idea is to get result from specific server and write back to Client but it doesn't close after sending .
pls have alook loop in line 30

====code ====

  public void run() {
 
    while (true) {
      try {
        Socket s = theServer.accept();        
        System.out.println("Client request is accepted!!!");                      
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);              
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()), 1024 );  
       
        while (true) {          
              
              try{        
                      upServer = new Socket("192.168.0.5",4444);
                    outS = new PrintWriter(upServer.getOutputStream(),true);
                    inS  = new BufferedReader(new InputStreamReader(upServer.getInputStream()),1024);
                      
                    if(upServer.isConnected()) {                              
                           outS.println(iCmd);
                           outS.flush();                           
                     }
                     
                     String n = in.readLine();          
                      if (n == null) break;    
                      System.out.println("Client said: " + n );  
                                                      
                      outS.println(n);
                      outS.flush();
                      
                      System.out.println(n + " were sent to upper server");  
                      
                      while ( (result_out = inS.readLine()) != null ) {      
                                                         
                            System.out.println(result_out);
                            out.println(result_out);                                                                 
                        /// if (result_out.indexOf("Total") != -1) break;   =====>>> it work if i search for specifix Values and break up!
                  }
                  out.flush();
               }
               catch (IOException ex) {
                        System.err.println(ex);    
               }
               finally{
                  outS.close();
                  inS.close();
            }      
        } // end while        
       
        out.close();
        in.close();
       
      } // end try
      catch (IOException ex) {
            System.err.println(ex);
      }
    }  
  }  

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

>>while ( (result_out = inS.readLine()) != null ) {    

Will block unless the other end closes the stream so the loop won't terminate
You will have to arrange the other end to either

a. close the stream
b. send an end of transmission message that can be caught in your loop
Avatar of ikhmer
ikhmer

ASKER

could you give me more detail please? i'm not realy understand >>> You will have to arrange the other end to either
how can i close stream? any methode that could know the stream\buffer is complete and ready to close?

thanks,
Oh - is this your echo server?
Avatar of ikhmer

ASKER

yesh, it related my previous post!

tha;nks,
Well, if it is, you need never end that loop
Avatar of ikhmer

ASKER

the listening part is keep to listen for connection request from client but above part is use to send back to client and
i want it s closed when finished

thanks
An echo server *never* closes (their loops don't finish). That's how they work
I already answered this in your preevious question.
you either need the upper server to close the connection once sdone.
or check ofor a line to indicate the end (as u have commentwed out)
Avatar of ikhmer

ASKER

yesh, i have to close the connectiont to upper server but the closing part would be under loop statement-- so, it doesn't reach to that yet
If you are still using this 'upper server' you just send what you read in the loop
(to *it*)
Avatar of ikhmer

ASKER

since i limited number of thread and as i have test if we can't close this loop the next process can't start (e.g i allow only 2 thread, so the 3rd connection will wait always)
Why not? The server is multithreaded is it not? The fact that one thread is still reading should not affect the ability of the server to spawn another thread
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
In an echo server, you are *never* finished (unless the client closes the connection). That's how they work
Avatar of ikhmer

ASKER

i limited the number of thread! as in my testing i allow only 2thread, so the 3rd won't accepted unless 1st or 2nd thread is closed
actually doesn't matter how many threads you have, you need to close the connection.
and to do that use whichever of the methods i suggested above suits your needs :)
I'll say this once more: the only way an echo server connection to a client closes is if the *client* closes the connection.

Its only function is to *echo* what the client sends and does not, and *cannot* know in advance what that's going to be
ikhmer - which part of the accepted answer are you actually using then?
Avatar of ikhmer

ASKER

hahaa :-)
i enable what i had turn off

if (result_out.indexOf("Total") != -1) break;   =====>>> it work if i search for specifix Values and break up!

Anyway, i'm really prefer this way but i have no choice!

and my first problem still not solved yet, i think it work with separetly connection but i found that the result of 2nd thread is show in 1st thread when i start to process at the same time! i will post my previouse code again!!!!

thanks,
>>if (result_out.indexOf("Total") != -1) break;   =====>>> it work if i search for specifix Values and break up!

It that's coming to the upper server from the server threads then of course you can use it. Why anyway would you *not* close it if all the info has been sent to the upper server?
Avatar of ikhmer

ASKER

i don't get you-- how can i close? if i don't break the loop the process will never reach to close statement... i will post my complete code to you both and guide me to the right way!