2017-11-12 1 views
1

私はソケットとスレッドを使ってjavaでプログラムを書いています。クライアントクラスでは、クライアントがサービスを追加してサーバーに送信できるメソッドがあります。サーバーはデータを受け取り、ArrayListに格納します。私の問題は、サービスを保存した後、クライアントにメッセージを送り返したいので、サービスが正常に追加されたことを知っているだろうが、残念ながら私が受け取っているのは空の文字列です。ここでサーバーが空の文字列をクライアントに送信するのはなぜですか?

は私のコードです:

//method from client class 

    private static void addServices() throws IOException{ 
    input= new BufferedReader(new InputStreamReader(System.in)); 
    inputClient=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
    output= new PrintWriter(clientSocket.getOutputStream()); 
    System.out.print("\nName of service: "); 
    String name=input.readLine(); 
    output.println(name); 
    output.flush(); 
    System.out.print("\nData of service: "); 
    String data=input.readLine(); 
    output.println(data); 
    output.flush(); 
    String getMsg; 
    getMsg=inputClient.readLine(); //here I got blank string from server 
    System.out.println(getMsg); 
} 


    //method from Server class 

    private void addServices()throws IOException{ 
    reader= new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
    output=new PrintWriter(clientSocket.getOutputStream()); 
    String name=reader.readLine(); 
    String data=reader.readLine(); 
    serviceList.add(new Service(login,name,true,data)); 
    String msg="\nService added succesfully."; 
    output.println(msg); // here's message I wanna sent to the client 
    output.flush(); 
    } 

あなたは私が間違ってやっと理由てるものを私に説明できますか?手伝ってくれてありがとう。

答えて

0

ここには何がありますか: メッセージ"\nService added succesfully."がクライアントに送信されました。次に、inputClient.readLine()メソッドは、0番目の文字と行終了文字の間の文字列を読み取ります。この文字は、メッセージ内の"\n"の前に空の文字列です。 \nを削除してください。メッセージが正しく取得されるはずです。

+0

これは現在動作します、ありがとうございます。 – xev19

関連する問題