Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

process of loop doesn't finished!

Posted on 2006-11-11
23
Medium Priority
?
214 Views
Last Modified: 2010-03-31
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);
      }
    }  
  }  

0
Comment
Question by:ikhmer
[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
  • 12
  • 8
  • 3
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920438
>>while ( (result_out = inS.readLine()) != null ) {    

Will block unless the other end closes the stream so the loop won't terminate
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920441
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
 

Author Comment

by:ikhmer
ID: 17920454
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17920472
Oh - is this your echo server?
0
 

Author Comment

by:ikhmer
ID: 17920475
yesh, it related my previous post!

tha;nks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920483
Well, if it is, you need never end that loop
0
 

Author Comment

by:ikhmer
ID: 17920508
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920518
An echo server *never* closes (their loops don't finish). That's how they work
0
 
LVL 92

Expert Comment

by:objects
ID: 17920521
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
 

Author Comment

by:ikhmer
ID: 17920528
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920533
If you are still using this 'upper server' you just send what you read in the loop
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920534
(to *it*)
0
 

Author Comment

by:ikhmer
ID: 17920539
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920547
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 17920753
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920759
In an echo server, you are *never* finished (unless the client closes the connection). That's how they work
0
 

Author Comment

by:ikhmer
ID: 17920766
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
 
LVL 92

Expert Comment

by:objects
ID: 17920775
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920776
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920891
ikhmer - which part of the accepted answer are you actually using then?
0
 

Author Comment

by:ikhmer
ID: 17920923
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17920930
>>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
 

Author Comment

by:ikhmer
ID: 17920941
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 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