2012-10-21 24 views
0

大家好,我建立一個看起來是這樣的一個多線程聊天服務器:同步處理資料多線程服務器

public class Main { 

    public static ServerSocket server; 
    public static Socket connection; 
    public static int backLog = 100; 
    public static int numberOfConnected; 
    public static boolean connected = false; 
    public final static int potNumber = 6080; 
    public static PrintWriter pw; 
    public static Scanner input; 
    public static int i = 0; 

    public static void main(String[] args) { 
     startServer(); 

    } 
    public static void startServer(){ 
     try { 
      server = new ServerSocket(potNumber, backLog); 
      waitingForConnection(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    private static void waitingForConnection() { 
     connected = false; 
     i++; 
     while (!connected) { 
      try { 
       if (connected) { 

       } 
       connection = server.accept(); 
       Server s = new Server(connection, pw = new PrintWriter(connection.getOutputStream()), input = new Scanner(connection.getInputStream())); 
       s.start(); 
       numberOfConnected++; 
       waitingForConnection(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

} 

的想法是,這是假設是一個聊天服務器,所以當一個連接到服務器時,它開始以下螺紋:

線程

public void run(){ 
    while (connection.isConnected()) { 
     if (input.hasNext()) { 
      String fullMessage = input.nextLine(); 
      if (fullMessage.equalsIgnoreCase("Connect")) { 
       connectHim(); 
      }else { 
       chatMessage(fullMessage); 
      } 
     } 

     } 
    } 

private void chatMessage(String fullMessage) { 
    String name = fullMessage.substring(0, fullMessage.indexOf(" ")); 
    String message = fullMessage.substring(fullMessage.indexOf(" "), fullMessage.length()); 
    pw.println(name+": "+message); 
    pw.flush(); 

} 
private void connectHim() { 
    String name = input.nextLine(); 
    pw.println(0); 
    pw.flush(); 
    pw.println(1); 
    pw.flush(); 
    pw.println(); 
    pw.flush(); 
    pw.println(name); 
    pw.flush(); 

} 

所以我的問題是:

如果綁定到線程1的用戶(這是一個示例),綁定到線程2的用戶向服務器發送消息,我將如何將該消息發送給綁定在線程1上的用戶?

回答

1

其中一個選項是使用HashtableHashMap(如果使用Map,只需撥打Collections.synchronizedMap(myMap))。當你開始新的線程,給他獨特的名字(例如用戶暱稱),並把它放到你的集合key - 線程名稱,和value - 線程作爲對象。

if the user that is bound to thread 1 (this is an example) and the user bound to thread 2 sends a message to the server how will i send that message to the user bound on thread 1?

例如你有user1user2user3。現在你建立3個線程,並把它們放到HashMap,如:

Map<String, Thread> threadMap = new HashMap<String,Thread>(); 
    threadMap = Collections.synchronizedMap(threadMap); 



    YourThread th1 = new YourThread(); 
      threadMap.put("user1", th); 

    YourThread th2 = new YourThread(); 
      threadMap.put("user2", th); 

    YourThread th3 = new YourThread(); 
      threadMap.put("user3", th); 

      .... 

    Set<String> userSet = threadMap.keySet(); 

    Iterator<String> it = userSet.iterator(); 

    Thread currThread = null; 

    while(it.hasNext()){ 
     String key = it.next(); 

     currThread = threadMap.get(key); 

     // do something with currThread   
    } 
+0

你會如何修改我的代碼來做到這一點? –

+0

每當你有新的客戶時,拿他的獨特的名字,並把'地圖'。如果'user1'向'user2'發送消息,則運行在循環中(在我的情況下'while'),並找到名爲'user2'的Thread。提取他的主題並將消息傳給他。 –

+0

反正使用Tomcat來管理客戶端。據我所知,Tomcat每秒處理大約800個客戶端。你不需要線程/映射...只需在Tomcat中設置一些'Servlet'並運行它 –