2014-04-17 188 views
0

我正在學習考試,並正在處理以下示例問題。我有一個簡單的服務器,我試圖連接到客戶端。連接看起來很好,但由於某種原因,這是我在客戶端控制檯中獲得的輸出。誰能告訴我爲什麼會發生這種情況?爲什麼我的服務器響應打印不正確?

注意:我也可以從客戶端訪問Message類(我沒有這樣做,因爲我不知道這將如何幫助,但我認爲這是解決此問題的關鍵),如果有人可以提出做什麼的建議。

此外,我所做的任何更改必須是客戶端上的代碼。服務器端代碼不能更改。我也附上了我的代碼。由於

Console Screenshot

public class Client { 
public static void main(String[] args) throws IOException { 
    Socket s = new Socket("localhost", 8999); // create a new socket 
    InputStream instream = s.getInputStream(); // Create input Stream obkect 
    OutputStream outstream = s.getOutputStream(); // Create output stream 
                // object 
    Scanner in = new Scanner(instream); // Create scanner object that takes 
             // the instream as an argument 
    PrintWriter out = new PrintWriter(outstream); // Create a printwriter 
                // outstream object 

    String request="GET/HTTP/1.0\n\n"; 
    out.print(request); // pass the request to the outstream 
    out.flush(); // moves all the data to the destination 
    String response = in.nextLine(); // response is the next line in the 
             // input stream 
    System.out.println("Receiving: " + response); // Print statement 

    s.close(); // close the socket 
} 
} 


public class TimeServer { 

private static Date currentDateAndTime() { 
    return new Date(); 
    // (An object of class Date comprises time and date) 
} 

public static void main(String[] args) { 
    try { 
     ServerSocket serverSocket = new ServerSocket(8999); 
     while (true) { 
      Socket socket = serverSocket.accept(); 
      try { 
       ObjectOutputStream stream = new ObjectOutputStream(
         socket.getOutputStream()); 
       Date dt = currentDateAndTime(); 
       Message m = new Message(dt); 
       stream.writeObject(m); 
       stream.close(); 
      } catch (IOException e) { 
       System.out.println(e.getMessage()); 
      } 
      try { 
       socket.close(); 
      } catch (IOException e) { 
       System.out.println(e.getMessage()); 
      } 
     } 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
} 
} 


class Message implements Serializable { 
//This helper class can be used both by server and client code 

Date timeAndDate; 
// (Objects of class Date comprise time & date) 

Message(Date timeAndDate) { 
this.timeAndDate = timeAndDate; 
} 

} 
+0

看來它是一個問題,IDE日食..請改變你的Eclipse的工作空間,並再次嘗試.. – niiraj874u

+0

當你說要改變你的意思是離開工作區的工作區,然後返回到相同的工作區並重新運行?如果是這樣,它沒有效果。我有許多其他項目在這個工作區中運行,沒有任何問題 - 不確定這是否意味着什麼。謝謝 – kellzer

+0

我的意思是說,改變你的工作空間到文件系統中的其他目錄(不是同一目錄)chk .. – niiraj874u

回答

1

您只需要修改客戶端:

Scanner in = new Scanner(instream); // Create scanner object that takes 
            // the instream as an argument 

ObjectInputStream in = new ObjectInputStream(instream); 

而且修復從輸入流中讀取替換,替換:

String response = in.nextLine(); 
System.out.println("Receiving: " + response); 

有:

Message response = (Message) in.readObject(); 
System.out.println("Receiving: " + response.timeAndDate); 
+0

感謝您的幫助!這工作。我明白我的錯誤。我把對象當作字符串來對待。謝謝! – kellzer

相關問題