Solved

process of loop doesn't finished!

Posted on 2006-11-11
23
206 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 51
mapBully challenge 6 93
wordappend challenge 8 88
Unexpected HttpURLConnection connection behavior 2 52
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now