开发者

Problem while sending a string from client to server in java

开发者 https://www.devze.com 2023-03-16 03:27 出处:网络
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.

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 th开发者_高级运维e 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 :)


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号