2011-06-27 8 views
1

クライアントからサーバへStringを送信しましたが、サーバに受信したときに初めて2度目には印刷されず、理由がわかりません。
クライアント側:クライアントからサーバへの文字列をJavaで送信中に問題が発生しました

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

サーバーサイド:

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

それでは、どのように私は初めてで、それを受け取ることができます。ここ
はコードがあります。助けてください !!!事前に
感謝:)

+2

「初回」などの意味は正確にはっきりしません。短くて完全なプログラム(1台のサーバー、1台のクライアント)が本当に役に立ちます。 http://tinyurl.com/so-hints –

+0

コードをもっと表示してください。 –

+0

@ Jon Skee文字列が複数回送信され、サーバー/クライアント側でループしているので、複数回受信されるためです。 –

答えて

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

は、あなたが効果的にサーバー上で印刷する前に二度読んでいる

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

ありがとう:)、あなたは最高です。 –

+0

答えがあなたの問題を解決した場合は、答えの左側にあるチェックマークをクリックしてそれを受け入れる必要があります。 –

+0

私はすでにそれをしています! –

2

でなければなりません。これを試してみてください:

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

ありがとうございました:) D –

関連する問題