2017-05-23 6 views
0

は、次の場合を想像読んクライアント:は方法:Javaのサーバソケットの応答と

クライアント - クライアントがサーバ サーバーにリクエストを送信し

サーバー接続をクライアント クライアントが答えを読み答える

クラスのクライアント:

public class Client extends Service{ 

    private String IP_ADDRESS; 
    private int PORT; 

    public void start(){ 
     l.info("Starting client for server at: "+IP_ADDRESS+":"+PORT); 
     //Initialization of the client 
     try { 
      cs=new Socket(IP_ADDRESS,PORT); 
     } catch (UnknownHostException e) { 
      l.error("Unkown host at the specified address"); 
      e.printStackTrace(); 
     } catch (IOException e) { 


     l.error("I/O error starting the client socket"); 
       e.printStackTrace(); 
      } 
     } 

     //Sends the specified text by param 
     public void sendText(String text){ 
      //Initializa the output client with the client socket data 
      try { 
       //DataOutputStream to send data to the server 
       toServer=new DataOutputStream(cs.getOutputStream()); 

       l.info("Sending message to the server"); 

       PrintWriter writer= new PrintWriter(toServer); 
       writer.println(text); 
       writer.flush(); 

      } catch (IOException e) { 
       l.error("Bat initialization of the output client stream"); 
       e.printStackTrace(); 
      } 
     //Should show the answers from the server, i run this as a thread 
     public void showServerOutput(){ 
      String message; 

      while(true){ 
       //If there are new messages 
       try { 
        BufferedReader br= new BufferedReader(new InputStreamReader((cs.getInputStream()))); 
        if((message=br.readLine())!=null){ 
         //Show them 
          System.out.println(message); 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

showServerOutput()は、いずれかを返すメソッドですサーバー

によって送られた答えは、その後、私のサーバ・クラスを使用すると、イムは、言葉でクライアントにメッセージを送信見ることができるように、次のコード

public class Server extends Service{ 
    public void startListenner(){ 
     l.info("Listening at port "+PORT); 
     while(true){ 
      // Waits for a client connection 
      try { 
       cs=ss.accept(); 
       l.info("Connection received: "+cs.getInetAddress()+":"+cs.getPort()); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       toClient= new DataOutputStream(cs.getOutputStream()); 
       PrintWriter cWriter= new PrintWriter(toClient); 

       //Send a confirmation message 
       cWriter.println("Message received"); 
       //Catch the information sent by the client 
       csInput=new BufferedReader(new InputStreamReader(cs.getInputStream())); 

       printData(); 
       toClient.close(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 
} 

を持っている:「メッセージが受信された」が、そのクライアントに示すことはありませんコンソール。どうしましたか?

EDITprintData()方法は

public void printData(){ 
    l.info("Printing message received"); 
    try { 
     System.out.println(csInput.readLine()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

このスレッドは何度も尋ねられました。関連スレッドをチェックしようとしましたか? – Omore

+0

はい、私はこれを書く前に少し研究をしましたが、それは私を助けませんでした。私は実際にそれについて考えていましたが、それほど尋ねられたスレッドを作成するという事実がありましたが、私のコードでは間違ったことを見つけられませんでした。この不必要なスレッドは残念です。 – JD0001

答えて

1

あなたprintData()方法が何をしているかわからないが、あなたは、サーバー上cWriter.flush()が欠落していないコンソールでクライアントから受信したメッセージを出力します

あなたが「メッセージを受け取った」というメッセージを印刷したら、 私はそれを理解しているので、あなたのメッセージを書いても、それをあなたのクライアントに送ることはありません。

+0

「フラッシュ」を追加するのを忘れてしまったので、私は何も送っていませんでした。 – JD0001

+0

すごく助かりました! – Kaiserbogey

関連する問題