2012-01-28 209 views
0

我正在修改關於如何創建接受多個客戶端的TCP聊天服務器的教程。我最終還會創建一個客戶端類,但到目前爲止,我正在使用TELNET進行測試。TCP聊天服務器

我希望服務器不斷檢查輸入,以便我可以使用關鍵字預先形成服務器功能。因此,字符串單詞「EXIT」斷開客戶端和字符串單詞「Name:」以打印「OK」。

這是我在想什麼,但它不工作:

public void run() 
    { 
     String line; 
     try  
     { 
      while(true) 
      { 
       if (input.readline("EXIT"))//Should close and remove client 
       { 
        clients.remove(this); 
        users.remove(name); 
        break; 
       } 
       if(input.readline("Name:"))//Should print OK with username 
       { 
        System.out.println("OK"); 
       } 
       boradcast(name,line); // method of outer class - send messages to all 
      }// end of while 
     } // try 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } // end of run() 
} 

}

這裏是整個服務器類

// Chat Server runs at port no. 9020 
import java.io.*; 
import java.util.*; 
import java.net.*; 
import static java.lang.System.out; 

public class TCPServer 
{ 
    Vector<String> users = new Vector<String>(); 
    Vector<HandleClient> clients = new Vector<HandleClient>(); 

    int PORT = 9020; 
    int NumClients = 10; 

    public void process() throws Exception 
    { 
     ServerSocket server = new ServerSocket(PORT,NumClients); 
     out.println("Server Connected..."); 
     while(true) 
     { 
     Socket client = server.accept(); 
     HandleClient c = new HandleClient(client); 
     clients.add(c); 
    } // end of while 
    } 

    public static void main(String ... args) throws Exception 
    { 
     new TCPServer().process(); 
    } // end of main 

    public void boradcast(String user, String message) 
    { 
     // send message to all connected users 
     for (HandleClient c : clients) 
      if (!c.getUserName().equals(user)) 
      { 
       c.sendMessage(user,message); 
      } 
    } 

    class HandleClient extends Thread 
    { 
    String name = ""; 
    BufferedReader input; 
    PrintWriter output; 

    public HandleClient(Socket client) throws Exception 
    { 
      // get input and output streams 
     input = new BufferedReader(new InputStreamReader(client.getInputStream())) ; 
     output = new PrintWriter (client.getOutputStream(),true); 
     output.println("Welcome to Bob's Chat Server!\n"); 
     // read name 
     output.println("Please Enter a User Name: "); 
     name = input.readLine(); 
     users.add(name); // add to vector 
     output.println("Welcome "+name+" we hope you enjoy your chat today"); 
     start(); 
    } 

    public void sendMessage(String uname,String msg) 
    { 
     output.println(uname + ":" + msg); 
    } 

    public String getUserName() 
    { 
     return name; 
    } 

    public void run() 
    { 
     String line; 
     try  
     { 
      while(true) 
      { 
       if (input.readline("EXIT")) 
       { 
        clients.remove(this); 
        users.remove(name); 
        break; 
       } 
       if(input.readline(name)) 
       { 
        System.out.println("OK"); 
       } 
       boradcast(name,line); // method of outer class - send messages to all 
      }// end of while 
     } // try 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } // end of run() 
    } // end of inner class 
} // end of Server 
+0

請根據「*不起作用*」進行展開。編譯錯誤?運行時錯誤?意外的功能? (你看到什麼,與你期望的是什麼?) – ziesemer 2012-01-28 19:48:03

+0

現在,這是我編譯時得到的結果: TCPServer.java:79:找不到符號 symbol:method readline(java.lang.String ) 位置:類java.io.BufferedReader中 \t \t如果(input.readline( 「EXIT」)) \t \t^ TCPServer.java:85:找不到符號 符號:方法的ReadLine(java.lang.String中) 位置:類java.io.BufferedReader中 \t \t如果(input.readline( 「姓名:」)) \t \t^ 2錯誤 – user1174834 2012-01-28 19:57:03

+0

您應該使用「WebSockets」代替聊天服務器。這是現代化的做法。 – djangofan 2012-01-28 20:01:33

回答

3

不知道到底是什麼你希望在輸入行等於當前用戶名時執行此操作,我認爲這更多你要找的是什麼:

public void run(){ 
     try{ 
      while(true){ 
       String line = input.readLine(); 

       if("EXIT".equals(line)){ 
        clients.remove(this); 
        users.remove(name); 
        break; 
       }else if(name.equals(line)){ 
        System.out.println("OK"); 
       } 
       boradcast(name, line); // method of outer class - send messages to all 
      }// end of while 
     } // try 
     catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } // end of run() 

這解決的幾個問題:

  • input.readline不是方法,而是input.readLine是 - 它不接受任何參數。 (這應該顯示爲一個編譯錯誤。)
  • 你從來沒有分配任何東西給line字符串。
  • 您正在多次閱讀該行。如果它與「EXIT」不匹配,您將讀取一條新行,與name進行比較 - 無論用戶輸入的是上一行。
+0

謝謝,這有很大的幫助。我試圖使用input.readLine我想我輸入錯了。 – user1174834 2012-01-28 21:07:40