Link to home
Start Free TrialLog in
Avatar of ikhmer
ikhmer

asked on

from client i can send only first request to server

Dear experts,

I'm writting client server application ...from client side i need to send serveral request to server atfter connection is established!
let say my first command need to submit to server and get result back to anylize and find out what will send next and next ... the problem is
when i get result back from my first request the next command seem not interact by server... what is wrong?

thanks,
Avatar of Ajay-Singh
Ajay-Singh

you need to flush the client request and make sure that it is read my the server completely and vice-versa.
Avatar of CEHJ
ikhmer - you seem to be posting quite a few questions and accepting answers, yet you're still asking fundamental questions ..?
Avatar of ikhmer

ASKER

i already use flush!!! but from server side seem does nothing with second request!

Well Cehj, if i know all about it i won't ask you any question and do you know why i pay for this site???
OK. You need to post some code (as little as possible)
You need to use HTTP keep-alives (client and server) if you want to process more requests per connection. Be aware that for keep-alives to work, the size of the response must be known when the header is sent (and sent along with the header).
By client server application, do you mean a HTTP based web application with Servlets JSPs etc.??
Avatar of ikhmer

ASKER

not use HTTP-- i'm using normal console mode! below is apart of my server side code:
from client i want to send more than one comment and getback result before sending next command.

  public void run() {
 
    while (true) {
          
      try {
        Socket mySocket = theServer.accept();        
        System.out.println("Client request is accepted!!!");                      
        PrintWriter out = new PrintWriter(coreSocket.getOutputStream(), true);              
        BufferedReader in = new BufferedReader(new InputStreamReader(coreSocket.getInputStream()), 1024 );  
                             
              try{                       
                     String n = in.readLine();                               
                      if (n == null) break;    
                      
                      System.out.println("Client said: " + n );
                      getInputVal = n.split(" ");
                      
                      xxx=getInputVal[0];
                      yyy =getInputVal[1];  
               }
               catch (IOException ex) {
                        System.err.println(ex);    
               }  
               
               //***get connection parameter to upper server (...)
                try{
                        myDb_pooling db_pooling= new myDb_pooling();
                      db_pooling.myConn();
                
                      getHost_para=db_pooling.get_host_para("444").trim();
                                            
                      getPara_val=getHost_para.split("@@@");                                            
                      for (j=0;j<getPara_val.length;j++){
                            System.out.println(j + ":::: " +getPara_val[j]);
                      }
                      
                      Socket upServer = new Socket(192.168.0.1,4444);
                    PrintWriter outS = new PrintWriter(upServer.getOutputStream(),true);
                    BufferedReader inS  = new BufferedReader(new InputStreamReader(upServer.getInputStream()),1024);
              
                      /** Login to Upperser(s) **/
                    if(upServer.isConnected()) {                              
                           outS.println("lgi user:iUser,pwd:ipwd");
                           outS.flush();                           
                     }
               
                     outS.println("chk info:id=\"" + yyy + "\";");
                      outS.flush();  
                      
                      while ( (upSvr_out = inS.readLine()) != null ) {                                                               
                            out.println(upSvr_out);                                                                 
                            out.flush();
                                                  
                        if (upSvr_out.indexOf("Total") != -1) break;
                  }
      
                  
                  outS.close();
                  inS.close();
                  upServer.close();
                      
                }
                catch (Exception e) {
                        e.printStackTrace(System.err);
                        System.exit(-1);
               }
                     
                     
        out.close();
        in.close();
        mySocket.close();
       
      }  
      catch (IOException ex) {
            System.err.println(ex);
      }
     }  
  }  
 
I see that you are reading data only once from client and you are not reading the data from the client processing it and closing the stream

Simple way to implement this is by the method all universal protocols follow.
i.e by sending "\r\n"
from the cleint side if you think all parameters are sent then send signal "\r\n"
on sevrer side if received "\r\n" then server knows everything has been received.
Also in above code there is an error you are reading data from cleint only once .

your code is as follows

First of all its not multi-thread model .

while(true)
->accpeting a connection
-> opening input/output streams to perform r/w operations
-> reading data from cleint
-> processing data
-> close all streams
} - end of while

this means you are reading only one line

while(true) { //main while
 ->accpeting a connection
-> opening input/output streams to perform r/w operations
  while(!='\r\n') { //inner while till you read complete data or one by one
 -> reading data from cleint
 -> process data
  }//end of inner while
  close streams
  close socket
}//main while end


To make this multi thread model
one you open accept a new connection spawn a new thread by passing accpted socket as parameter to the new thread class.
this would be more efficient .


Hope this helps -
bond
Avatar of ikhmer

ASKER

in main class already extends thread! and if i use >>  while(!='\r\n') it get only first command and loop forever!!!
your client should send '\r\n'
after you read data put an explict condition or add this method which reads data from input stream and returns string

 public String readRequest() throws IOException{
      String request = "";
      for(int b; (b = in.read()) != -1;) {
         if((char) b == '\r');
         else if((char) b == '\n') {
            break;
         } else request += (char) b;
      }
      return request.trim();
   }


ASKER CERTIFIED SOLUTION
Avatar of bondzoro
bondzoro

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 ikhmer

ASKER

thank for your good idea! and if i have two while(true) , eg:

while(true) {  // #1
...
...
  while(true){  //#2
  ...
     if ( )  break; // want to break out from #2 only

  ...
  }
...
...
}


--> if i issue break statement in #2 it will exit from main while (#1) also! how to break from #2 only? so, the next process will keep running on while #1(main while)
Here is a simple class which illustrates how to break inner/outer loops..

class x {
   public static void main(String a[]){
      int i=0;
      int j=0;
outer: while(true) {
          System.out.println("outer");
          if(i==2) {
             System.out.println("breaking outer");
             break outer;
          }
inner: while(true) {
          System.out.println("inner "+j+":"+i);
          j++;
          if(j==2){
             i++;
             System.out.println("breaking inner i:" +i + ":j"+j);
             j=0;
             break inner;
          }
       }//inner
       }//outer
   }//main
}//class x

Hope this helps
Avatar of ikhmer

ASKER

Many thanks!!!