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

オプションの1つは、Hashtableまたはを使用することです(使用する場合はHashMap呼び出すだけです)。新しいスレッドを開始するときは、彼に一意の名前(たとえば、ユーザーのニックネーム)を付けて、コレクションに配置します。ここで、-スレッド名、および-オブジェクトとしてのスレッド。 Collections.synchronizedMap(myMap)Mapkeyvalue

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ます。次に、3つのスレッドを作成し、次のように配置します。user2user3HashMap

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 に答える