2012-04-05 175 views
1

服務器應該向客戶端發送消息「Hello :: enter QUIT退出」,然後客戶端鍵入任意文本,服務器回顯客戶端的文本,在他們之前添加「From server:」信息。 但似乎有一個混亂的順序,我似乎無法找到的地方!我一整天都在這裏!客戶端 - 服務器TCP通信

這是服務器的代碼:

import java.net.*; 


public class Server { 

    public static void main(String[] args) { 

     int nreq = 1; 
     try 
     { 
      ServerSocket sock = new ServerSocket (8080); 
      for (;;) 
      { 
       Socket newsock = sock.accept(); 
       System.out.println("Creating thread ..."); 
       Thread t = new ThreadHandler(newsock,nreq); 
       t.start(); 
      } 
     } 
     catch (Exception e) 
     { 
      System.out.println("IO error " + e); 
     } 
     System.out.println("End!"); 
    } 
} 

ThreadHandler代碼:

import java.io.*; 
import java.net.*; 

class ThreadHandler extends Thread { 
    Socket newsock; 
    int n; 

    ThreadHandler(Socket s, int v) { 
     newsock = s; 
     n = v; 
    } 

    // @SuppressWarnings("deprecation") 
    public void run() { 
     try { 

      PrintWriter outp = new PrintWriter(newsock.getOutputStream(), true); 
      BufferedReader inp = new BufferedReader(new InputStreamReader(
        newsock.getInputStream())); 
      outp.println("Hello :: enter QUIT to exit"); 
      boolean more_data = true; 
      String line; 
      while (more_data) { 
       line = inp.readLine(); 
       if (line == null) { 
        more_data = false; 
       } else { 
        outp.println("From server: " + line + "\n"); 
        if (line.trim().equals("QUIT")) 
         more_data = false; 
       } 
      } 
      newsock.close(); 
     } catch (Exception e) { 
      System.out.println("IO error " + e); 
     } 

    } 
} 

和客戶端代碼:

import java.io.*; 
import java.net.*; 
import java.util.Scanner; 

public class Client { 
    // @SuppressWarnings("deprecation") 
    public static void main(String args[]) { 
     Scanner scanner = new Scanner(System.in); 
     try { 
      Socket s = new Socket("localhost", 8080); 
      PrintWriter outp = new PrintWriter(s.getOutputStream(), true); 
      BufferedReader inp = new BufferedReader(new InputStreamReader(
        s.getInputStream())); 
      boolean more_data = true; 
      System.out.println("Established connection"); 
      String line; 
      while (more_data) { 
       line = inp.readLine(); 
       String userInput = scanner.nextLine(); 
       outp.println(userInput); 
       if (line == null) { 
        more_data = false; 
       } else 
        System.out.println(line); 
      } 
      System.out.println("end of while"); 
     } catch (Exception e) { 
      System.out.println("IO error " + e); 
     } 
    } 
} 

我測試出來我好後將使客戶成爲Android手機 - 如果可能的話 -


更新:

我已經改變了服務器的代碼:

outp.println("Hello :: enter QUIT to exit \n"); 
     boolean more_data = true; 
     String line; 
     while (more_data) { 
     line = inp.readLine(); 
     System.out.println("Message '" + line + "' echoed back to client.");// !! 
     if (line == null) { 
      System.out.println("line = null"); 
      more_data = false; 
     } else { 
      outp.println("From server: " + line + ". \n"); 
      if (line.trim().equals("QUIT")) 
       more_data = false; 
     } 
    } 
    newsock.close(); 
    System.out.println("Disconnected from client number: " + n); 

,並在Hello消息路易斯·米格爾·塞拉諾的末尾添加「\ n」的建議,並改變了客戶的方如下:

boolean more_data = true; 
     System.out.println("Established connection"); 
     String line;// = inp.readLine(); 

     while (more_data) { 
      line = inp.readLine(); 
      System.out.println(line); 
      if (line == null) { 
       // nothing read 
       more_data = false; 
      } else 
       line = inp.readLine(); 
      System.out.println(line); 
      String userInput = scanner.nextLine(); 
      if (userInput.trim() == "QUIT") { 
       s.close(); 
       System.out.println("Disconnected from server."); 
       more_data = false; 
      } else 
       outp.println(userInput); 

     } 
     System.out.println("end of while"); 

現在它工作正常。

如果任何人可以建議我一些Android客戶端-java服務器教程將不勝感激。

+0

你會得到哪些行爲?哪些錯誤(如果有的話)? – 2012-04-05 21:46:00

+0

沒有錯誤,但消息的順序混淆了。服務器應該發送「Hello :: .....」,那麼客戶端應該輸入一條消息,以便服務器迴應它。但它等待客戶端輸入消息,然後發送「Hello :: ....」消息 – 2012-04-05 21:47:56

回答

1

您的評論的順序,它可能是一個潮紅的問題。嘗試添加下面一行:

outp.flush(); 

後:

outp.println("Hello :: enter QUIT to exit"); 

當你寫一個流,有時你寫的東西都保存在一個緩衝區。如果你想確保緩衝區被清空並且字符串被實際發送,你需要調用flush()方法。

更新

此外,「\ n」添加到從服務器的歡迎你好消息的結尾。我認爲這會使它工作。

+0

它仍然在做同樣的事情!無法弄清楚爲什麼服務器的消息不會發送到客戶端! - 無論如何感謝您 – 2012-04-05 22:07:30

+0

將「\ n」添加到您的Hello字符串的末尾。我敢打賭是這樣。剛剛發生在我身上:) – 2012-04-05 22:09:20

+0

謝謝路易斯。這有助於:)現在工作正常。 – 2012-04-06 10:34:42