2012-07-19 76 views
2

我想在java中使用套接字編寫客戶機 - 服務器系統,但是我似乎無法讀取從服務器發送到客戶機的數據。從套接字讀取時獲取空字符串

下面是客戶端的代碼:

public class ClientSocket 
{ 
    Socket clientSocket = null; 
    PrintWriter out = null; 
    BufferedReader in = null; 


    // establish a connection to All Care's server application through socket 4444 (adjust localhost to reflect the IP address that the server 
    // is being run from) 
    public ClientSocket() 
    { 
     try 
     { 
      clientSocket = new Socket("localhost", 4445); 

      out = new PrintWriter(clientSocket.getOutputStream()); 
      in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not connect to All Care Server Application"); 
     } 
    } 

    public void closeClientSocket() 
    { 
     try 
     { 
      clientSocket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not close connection to All Care Server Application"); 
     } 
    } 

    public String getMessageFromServer() 
    { 
     try 
     { 
      String input = in.readLine(); 

      return input; 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not read message from server"); 
     } 

     return "No Data"; 
    } 

    public void sendMessageToServer(String message) 
    { 
     out.write(message); 
    } 
} 

這裏是服務器代碼:

public class ArFileServer { 

    public static void main(String[] args) 
    { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 

     try 
     { 
      serverSocket = new ServerSocket(4445); 

      // infinite loop to continually listen for connection requests made by clients 
      while (listening) 
      { 
       new ClientConnection(serverSocket.accept()).start(); 

       if (serverSocket != null) 
       { 
        System.out.println("Connection to client established"); 
       } 
      } 

      serverSocket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Error could not create socket connection to port"); 
     } 
    } 
} 

public class ClientConnection extends Thread 
{ 
    private Socket socket = null; 

    public ClientConnection(Socket socket) 
    { 
     super("ClientConnection"); 
     this.socket = socket; 
    } 

    // the thread that runs after a connection to the server has been accepted 
    @Override 
    public void run() 
    { 
     try 
     { 
      PrintWriter out = new PrintWriter(socket.getOutputStream()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

      sendMessagetoClient(out, "CONNECTION SUCCESS"); 

      // check login credentials sent from client to the server 

      // if valid send back their encrypted password, otherwise output a login error message 

      // wait for user input and then do various processes based on their requests 

      in.close(); 
      out.close(); 
      socket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Client socket connection error"); 
     } 
    } 

    // sends a message to the client 
    void sendMessagetoClient(PrintWriter out, String message) 
    { 
     out.write(message); 
    } 

    // listens for a message from the client 
    String getMessageFromClient(BufferedReader in) 
    {  
     try 
     { 
      String input = in.readLine(); 
      return input; 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not read message from client"); 
     } 

     return "No Data"; 
    } 

這裏是使用的是看代碼IM的行,如果被髮送的數據。

System.out.println(clientSocket.getMessageFromServer()); 

回答

2

在你sendMessageToClient()方法,你需要刷新:

void sendMessagetoClient(PrintWriter out, String message) 
{ 
    out.write(message); 
    out.flush(); 
} 

或者,當您創建PrintWriter,使用帶有autoflush構造:

PrintWriter out = new PrintWriter(socket.getOutputStream(),true); 

而當你寫,而不是out.write(message)使用printf()println()

+0

像一個魅力一樣工作! autoflush會工作嗎? – 2012-07-19 03:55:07

+0

@MatthewPigram是的,我編輯的答案包括作爲一個可能的修復。但是調用'out.write()'不會自動刷新,你需要調用'printf()'或'println()'來自動刷新。 – 2012-07-19 03:59:08

+0

啊謝謝一堆,我曾嘗試使用println,並想知道爲什麼它是npt工作,這是因爲我不自動刷新!泰 – 2012-07-19 04:05:34

-2

在克林特代碼,你不與服務器套接字連接。 爲克林特套接字連接

socket soc= new socket ("server host ip",port); 
+1

見行'ClientSocket的()'構造函數:'ClientSocket的=新的Socket( 「localhost」 的,4445);' – 2012-07-19 03:46:04

+1

葉I DO連接,我甚至得到服務器進行打印時,連接接受 – 2012-07-19 03:47:56

+1

要清楚ClientConnection是服務器代碼(即當客戶端連接被接受時由服務器運行的線程) – 2012-07-19 03:50:39

0

這裏有幾個問題。

  1. 您正在閱讀的文章,但您並未撰寫文章。

  2. 您不檢查readLine()的結果爲null,這意味着對等關閉了連接,這意味着您必須同樣做。

  3. 寫完後,您並沒有沖洗PrintWriter

  4. 您正在以錯誤的順序關閉東西。您必須關閉已連接到套接字的輸出寫入器/流。這樣做會刷新它,然後關閉輸入流/閱讀器和套接字。以錯誤的順序執行此操作會丟失同花。一旦你關閉了輸出,你就不需要另外兩個關閉。

  5. 您正在使用PrintWriter,,它通過網絡吞服異常,您需要了解通信中的異常和錯誤,並且您也不檢查錯誤。使用BufferedWriter.