2017-03-25 8 views
0

私は基本クライアントをJavaで作成しようとしています< - >サーバー接続。サーバーに書き込もうとすると、クライアントは詳細を正しく送信し、クライアント出力ストリームがクローズされるまでサーバーは読み取りをストールします。しかし、一旦出力ストリームが閉じられると、明らかにソケットが閉じられ、そのためにサーバはクライアントに返答することができません。この相互作用を処理するコードの主な部分を次に示します。Javaソケット:クライアント・サーバーがクローズされるまでサーバーは入力を読み取らない - >サーバーがクライアントに応答できない

クライアント:

private void sendCmd(String cmd) { 
    String infoToSend = cmd; 
    try { 
     socket = new Socket(hostname, port); 
     System.out.println("Trying to send: " + com.sun.org.apache.xml.internal.security.utils.Base64.encode(infoToSend.getBytes())); 
     out = new DataOutputStream(socket.getOutputStream()); 
     out.writeBytes(com.sun.org.apache.xml.internal.security.utils.Base64.encode(infoToSend.getBytes())); 
     out.flush(); 
     System.out.println("Socket is flushed"); 


     System.out.println("Waiting for Data"); 
     InputStream is = socket.getInputStream(); 
     System.out.println("Trying to get data"); 
     BufferedReader input = new BufferedReader(
       new InputStreamReader(is) 
     ); 

     String line; 
     while((line = input.readLine()) != null) { 
      System.out.println(line); 
     } 
     socket.close(); 
    } catch (IOException e) { e.printStackTrace(); } 
} 

サーバー:

public void run() { 
    System.out.println("Got Connection"); 
    try { 
     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     out = new DataOutputStream(socket.getOutputStream()); 

     String response; 
     System.out.println("Response:"); 
     String decode = ""; 
     while ((response = in.readLine()) != null) { 
      try { 
       decode = new String(Base64.decode(response)); 
      } catch (Base64DecodingException e) { 
       e.printStackTrace(); 
      } 
     } 


     System.out.println("Decoded: " + decode); 
     out.writeBytes("We got your message!"); 
     out.flush(); 
     out.close(); 
    } catch (IOException e) { System.out.println("Fail"); e.printStackTrace(); } 

誰もがこのエラーを解決する方法に私を導くことができるだろう。申し訳ありませんが、超簡単で、私はそれを見ることができません。送信

socket.shutdownOutput(); 

+0

ここで、デコード変数を出力しますか? – Omore

+0

@Omoreこれをデバッグしようとするとコンソールになるだけです – Nom

答えて

0

は、問題を解決しました。

関連する問題