2012-01-30 132 views
1

我正在研究一個簡單的java tcp聊天服務器。用戶可以輸入「push:」,「get」,「adios」,「help」等命令。 。 。我通過設置String Line = input.readLine()來檢查這些用戶命令(如下所示)。問題是當我輸入「push:」時,我必須去換一個新的行來獲取新的輸入並將其分配給一個新的字符串。我希望能夠在用戶輸入命令後在同一行上獲得輸入。從同一行讀取多個輸入。

例如:如果用戶1種類型在「推送」 +「你好,你今天怎麼」我想存儲「你好,今天好嗎?」到一個數組,以便以後可以打印它的內容。但現在,我必須輸入「push:」,然後輸入「Hello,今天好嗎?」將其存儲到數組中。這只是尷尬。有什麼方法將輸入存儲到同一行上的兩個不同的字符串?

這裏是我的服務器代碼,檢查運行方法

// 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>(); 
    Vector<String> Chat = new Vector<String>(); 

    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) 
    { 
     System.out.println(user + ": " + 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 Alan's Chat Server!\n"); 
     // read name 
     output.println("Please Enter a User Name: "); 
     name = input.readLine(); 
     users.add(name); // add to vector 
     output.println("\nWelcome "+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) 
      { 
         line = input.readLine(); 
      line.toLowerCase();//check this 
       if("adios".equals(line)) 
      { 
       output.println("\nClosing Connection . . . Goodbye"); 
          clients.remove(this); 
         users.remove(name); 
       break; 
        } 
      else if(name.equals(line)) 
      { 
          output.println("OK"); 
        } 
      else if("help".equals(line)) 
      { 
       output.println("\nServer Commands:" + "\n\"audios:\" close the client connection\n"+ 
               "\"Name:\" display \"OK\"\n");//Check this 
      } 
      else if("push: ".equals(line)) 
      { 
       String NewLine = input.readLine(); 
       Chat.add(NewLine); 
       output.println("OK"); 
      } 
      else if("get".equals(line)) 
      { 
       output.println(Chat.toString()); 

      } 
      else 
      { 
        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 

回答

1

可能與String.split("")或使用StringTokenizer

String line = input.read(); 
String [] d = s.split(":",2); 

通過數組索引訪問值;

編輯

爲清楚起見,我經過2作爲分割方法的第二個參數,因爲它限制了分裂一個陣列,只留下兩個指標,所以你不要有3個陣列這樣的事情:

String.split("PUSH: I am hungry :P");,因爲它會輸出[PUSH;我餓了; P]

+0

看起來不錯,謝謝。 – user1174834 2012-01-30 20:10:31

1
line = input.readLine(); 
line.toLowerCase();//check this 
.... 
else if("push: ".equals(line)) 
..... 

應該

line = input.readLine(); 
String command = line.split(":")[0]; 
String otherStuff = line.split(":")[1]; 
command.toLowerCase();//check this 
.... 
if("push".equals(command)); 
..... 
+0

這看起來不錯,我擡頭分裂,但我不知道如何使用它。 – user1174834 2012-01-30 20:10:11