1

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 :)

4
  • 2
    It's really unclear exactly what you mean by "in the first time" etc. A pair of short but complete programs (one server, one client) would really help. Please read tinyurl.com/so-hints Commented Jun 27, 2011 at 14:58
  • Please show more of your code. Commented Jun 27, 2011 at 14:59
  • @Jon Skee because the string will be sent multiple times and received multiple times since it is in a loop in the server/client sides. Commented Jun 27, 2011 at 15:11
  • In the first piece of code, nothing's ever changing str - do it doesn't make sense to be in a loop checking the contents of str... as I say, a short but complete pair of programs would really help. Commented Jun 27, 2011 at 15:14

2 Answers 2

3
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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

If the answer solved your problem, you should click the checkmark to the left of the answer to accept it.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.