Mohannad.Z
Mohannad.Z

Reputation: 47

Problem while sending a string from client to server in java

I have sent a String from client to server but when it receives to the server it doesn't print in the first time only in second time and don't know why.
Here is the code :
Client Side:

String str = "40D32DBBE665";  
while (str != null)   {  
    out.writeUTF(str);  
    }  

Server Side:

String st="";  
while(in.readUTF()!=null){ // it gets into the loop in the first time 
            System.out.println("Test"); // in the first time it prints this line 
            st = in.readUTF();  
            System.out.println(st);  
          }  

So how I can receive it in the first time. Please help !!!
Thanks in advance :)

Upvotes: 1

Views: 489

Answers (2)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

You are effectively reading twice before printing on the server. Try this:

  while((st = in.readUTF())!=null){ // it gets into the loop in the first time               
        System.out.println(st);  
      } 

Upvotes: 2

Prince John Wesley
Prince John Wesley

Reputation: 63708

while(in.readUTF()!=null){ // it gets into the loop in the first time 
            System.out.println("Test"); // in the first time it prints this line 
            st = in.readUTF(); 

should be

while((st=in.readUTF())!=null){ // don't miss odd messages
     System.out.println(st);
}

Upvotes: 3

Related Questions