简体   繁体   English

在Java中从客户端向服务器发送字符串时出现问题

[英]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 :) 提前致谢 :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM