2010-08-01 54 views
3

這是代碼。幫助第一個網絡程序

public class testClient { 
public static void main(String[] args) { 
    testClient abc = new testClient(); 
    abc.go(); 
} 
public void go() { 
    try { 
     Socket s = new Socket("127.0.0.1", 5000); 
     InputStreamReader sr = new InputStreamReader(s.getInputStream()); 
     BufferedReader reader = new BufferedReader(sr); 
     String x = reader.readLine(); 
     System.out.println(x); 
     reader.close(); 
    } catch(IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 

public class testServer { 
public static void main(String[] args) { 
    testServer server = new testServer(); 
    server.go(); 
} 
public void go() { 
    try { 
     ServerSocket s = new ServerSocket(5000); 
     while(true) { 
      Socket sock = s.accept(); 

      PrintWriter writer = new PrintWriter(sock.getOutputStream()); 
      String toReturn = "No cake for you."; 
      writer.println(toReturn); 
     } 
    } catch(IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 

java.io.*java.net.*都是進口的這兩類。

現在,當我嘗試運行這些(使用不同的終端)時,沒有任何反應。我究竟做錯了什麼?

屏幕:http://i29.tinypic.com/250qlmt.jpg

+2

如果你telnet到它,你的服務器是否工作? 'telnet localhost 5000'應該告訴你。然後你只需要一次調試一半。 – 2010-08-01 11:38:57

+0

(注意,你選擇的任何字符編碼恰好是默認的雙方,明確指定編碼,可能是UTF-8更好,同樣'PrintWriter'處理異常,所以你可能想考慮一個不同的'Writer'。) – 2010-08-01 11:45:59

回答

5

使用PrintWriter時,您需要調用flush方法。將代碼更改爲以下內容,並且工作:)。

public class testServer { 
public static void main(String[] args) { 
    testServer server = new testServer(); 
    server.go(); 
} 
public void go() { 
    try { 
     ServerSocket s = new ServerSocket(5000); 
     while(true) { 
      Socket sock = s.accept(); 

      PrintWriter writer = new PrintWriter(sock.getOutputStream()); 
      String toReturn = "No cake for you."; 
      writer.println(toReturn); 
      writer.flush(); 
     } 
    } catch(IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 
5

您的輸出顯然是緩衝。寫你的輸出後,嘗試刷新:

 writer.println(toReturn); 
     writer.flush(); 

此外,您還可以考慮在你的服務器調用sock.close()如果你使用的插座做,否則客戶端會在想下一步該怎麼做。

+0

Writer.close()會更重要,因爲它也會完成flush()。 – EJP 2010-08-01 12:57:55

+0

我永遠不會記得是否調用類似PrintWriter.close()的函數關閉了底層的OutputStream。 – 2010-08-01 13:02:33

+0

它的確如此,但是你必須成功地創建了'PrintWriter',所以你不妨關閉底層的流,不管怎麼樣(在給裝飾者一個滿意的例子之後)。對於套接字,默認關閉其中一個流實際上會關閉套接字。 – 2010-08-01 13:18:29

1

看看這個簡單的工作示例。

import java.net.*; 
    import java.io.*; 
    public class Server { 
     Server() throws IOException { 
      ServerSocket server = new ServerSocket(4711); 
      while (true) { 
       Socket client = server.accept(); 
       InputStream in = client.getInputStream(); 
       OutputStream out = client.getOutputStream(); 
       int multiplikator1 = in.read(); 
       int multiplikator2 = in.read(); 
       int resultat = multiplikator1 * multiplikator2; 
       out.write(resultat); 
     } 
     public static void main (String[] args) { 
     try { 
     Server server = new Server(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

    import java.net.*; 
    import java.io.*; 
    public class Client { 
     Client() throws IOException { 
      Socket server = new Socket ("localhost", 4711); 
      InputStream in = server.getInputStream(); 
      OutputStream out = server.getOutputStream(); 
      out.write(4); 
      out.write(9); 
      int result = in.read(); 
      System.out.println(result); 
      server.close(); 
     } 
     public static void main (String[] args) { 
      try { 
      Client client = new Client(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
1

關閉作者。