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 上的用户?

4

1 回答 1

1

选项之一是使用HashtableHashMap(仅Collections.synchronizedMap(myMap)Map使用时调用)。当您启动新线程时,给他唯一的名称(例如用户昵称)并将其放入您的集合中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?

例如,您有user1, user2, user3。现在您构建 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         
    }
于 2012-10-21T19:22:42.737 回答