2016-06-19 4 views
0

こんにちは、stackoverflowコミュニティ、Javaサーバークライアント通信が固まっているようです

Javaでのソケット通信に関する問題が発生しました。ここで

は私のサーバーとクライアントクラスのサンプルコードです:

サーバー:

public class OTPServer { 


static ServerSocket serverSocket; 
final static int PORT = 4242; 
static Socket clientConnection; 

public static void main(String[] args) { 

    try { 
     serverSocket = new ServerSocket(PORT); 
     System.out.println("Socket initialized"); 
     String serverMessage = "Hello, I am the Host"; 
     ServerTool serverTool = new ServerTool(); 

     while (true) { 
      clientConnection = serverSocket.accept(); 
      if(clientConnection.isConnected()) { 
       System.out.println("Client connected"); 
      } 

      BufferedReader clientInputReader = new BufferedReader(new InputStreamReader(clientConnection.getInputStream())); 
      DataOutputStream serverOutput = new DataOutputStream(clientConnection.getOutputStream()); 
      System.out.println("Sending message to client: " + serverMessage); 
      serverOutput.writeBytes(serverTool.encodeMessage(serverMessage)); 
      serverOutput.flush(); 
      String clientMessage = clientInputReader.readLine(); 
      System.out.println("Encoded answer from client: " + clientMessage); 
      String decodedMessage = serverTool.decodeMessage(clientMessage); 
      System.out.println("Decoded answer from client: " + decodedMessage); 

      serverOutput.close(); 
      clientInputReader.close(); 

     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    System.out.println("Hello, I am the OTP Server!"); 

} 

ここでは、クライアントである:

public class OTPClient { 

static Socket clientSocket; 
final static int PORT = 4242; 
final static String HOST = "localhost"; 


public static void main(String[] args) { 
    System.out.println("I am the OTP Client!"); 
    String serverMessage; 
    String clientResponse = "I am the Client"; 
    OTPTool otpTool = new OTPTool(); 

    try { 

     clientSocket = new Socket(HOST, PORT); 
     BufferedReader serverInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
     DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream()); 
     System.out.println("Connection to Host established"); 
     serverMessage = serverInput.readLine(); 
     System.out.println("Encoded Message from Server: " + serverMessage); 
     String decodedMessage = otpTool.decodeMessage(serverMessage); 
     System.out.println("Decoded message from Server: " + decodedMessage); 
     System.out.println("Answering with own message: " + clientResponse); 
     outputStream.writeBytes(clientResponse); 
     outputStream.flush(); 
     outputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

は今どこに私の問題は、次のとおりです。

接続が確立され、サーバーはクライアントにメッセージを送信しているように見えます。答え。クライアントは、サーバーから取得したメッセージを印刷しません。 サーバーをキャンセルするとすぐに、クライアントはサーバーから取得したメッセージと情報を出力します。応答は送信終了コード0で終了しますので、この部分は何とか止まっているようです。 与えられたサンプルコードで見てきたように、すでにoutputStreamをフラッシュしようとしました。

何か不明な点がありますか? これは本当に基本的なものですが、通信用にソケットを初めて使用したことがあります。

ありがとうございます!

よろしく、 ロニー

はところで:私は、サーバーのみの接続を要求して一つのクライアントに接続されていることを知っています。それは私の使用には絶対に十分です。

+0

デッドロック/並行性の問題です。これらのクラスをそれぞれどのような順序で実行しますか? – vontell

+0

あなたはreadLineを呼び出しています。あなたの 'serverTool.encodeMessage'はメッセージの最後に改行を含んでいますか? –

+0

@BrettOkken現時点では、 'serverTool.encodeMessage'はパラメータ文字列を返します。 @vontellまずサーバとクライアントを起動します。サーバはクライアントが接続するのを待っているので問題はありません。 – LongRon

答えて

1

改行またはファイルの終わりに遭遇するまで、serverInput.readLine();がブロックされるため、機能が停止しています。サーバー側では、改行を送信していないので、クライアントはブロックします。

+0

ありがとう!私は、通常のString戻り値にはどちらも含まれていないことはわかりませんでした。今すぐ完璧に動作します。改行やファイルの終わりを使わずに入力を読み取る方法はありますか? – LongRon

+0

'readLine'を呼び出さないでください。他の読み取り方法の1つを使用します。これには、メッセージの開始/停止を定義する「プロトコル」を定義する必要があります。 –

関連する問題