开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜