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);
      }
    }  
  }  

ikhmerAsked:
Who is Participating?
 
objectsCommented:
> 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

no, i meant the upper server closes the connection once its done sending results.

alternatively you need to know when you are finished (as in the code u have commented out)
0
 
CEHJCommented:
>>while ( (result_out = inS.readLine()) != null ) {    

Will block unless the other end closes the stream so the loop won't terminate
0
 
CEHJCommented:
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ikhmerAuthor Commented:
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,
0
 
CEHJCommented:
Oh - is this your echo server?
0
 
ikhmerAuthor Commented:
yesh, it related my previous post!

tha;nks,
0
 
CEHJCommented:
Well, if it is, you need never end that loop
0
 
ikhmerAuthor Commented:
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
0
 
CEHJCommented:
An echo server *never* closes (their loops don't finish). That's how they work
0
 
objectsCommented:
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)
0
 
ikhmerAuthor Commented:
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
0
 
CEHJCommented:
If you are still using this 'upper server' you just send what you read in the loop
0
 
CEHJCommented:
(to *it*)
0
 
ikhmerAuthor Commented:
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)
0
 
CEHJCommented:
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
0
 
CEHJCommented:
In an echo server, you are *never* finished (unless the client closes the connection). That's how they work
0
 
ikhmerAuthor Commented:
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
0
 
objectsCommented:
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 :)
0
 
CEHJCommented:
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
0
 
CEHJCommented:
ikhmer - which part of the accepted answer are you actually using then?
0
 
ikhmerAuthor Commented:
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,
0
 
CEHJCommented:
>>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?
0
 
ikhmerAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.