首页 > 解决方案 > 适用于所有客户端的主要 LinkedHashMap

问题描述

我有一个带有 ServerThreads 的 ServerMain 类。每次客户端连接时,都会创建 1 个新的 ServerThread。我希望每个客户端都发送一个字符串 [],其中包含保存在 LinkedHashMap 中的端口和坐标,然后服务器应该向客户端发送 LinkedHashMap 以及其他客户端的所有端口和坐标。但事实证明,每个客户端都将 string[] 发送到不同的 ServerThread,因此不同的 LinkedHashMap。我怎样才能让每个客户端都发送到一个 LinkedHashMap,而服务器向所有客户端发送这个 LinkedHashMap?

public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea chatWindow;
private List<Integer> ports = new ArrayList<Integer>();

public Main() throws IOException {
    super("ServerConsole");

    chatWindow = new JTextArea();
    chatWindow.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(chatWindow);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setBounds(0, 20, 596, 200);
    add(scrollPane);

    setLayout(null);
    setSize(600, 300);
    setResizable(false);
    setVisible(true);
    getContentPane().setBackground(Color.white);

    Socket s = null;
    ServerSocket ss2 = null;
    showMessage("Server Listening......");
    try {
        ss2 = new ServerSocket(3175); // can also use static final PORT_NUM
                                        // , when defined

    } catch (IOException e) {
        e.printStackTrace();
        showMessage("Server error");

    }

    while (true) {
        try {
            s = ss2.accept();
            showMessage("connection Established\n");
            ports.add(s.getPort());
            ServerThread st = new ServerThread(s);
            st.start();

        }

        catch (Exception e) {
            e.printStackTrace();
            showMessage("Connection Error");

        }
    }

}

private void showMessage(final String m) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            chatWindow.append(m);
        }
    });

}

}

class ServerThread extends Thread {

private ObjectOutputStream output;
private ObjectInputStream input;
Socket s = null;
private LinkedHashMap<Integer, String> playerCoords = new LinkedHashMap<Integer, String>();

public ServerThread(Socket s) {
    this.s = s;
}

public void run() {
    try {
        input = new ObjectInputStream(s.getInputStream());
        output = new ObjectOutputStream(s.getOutputStream());

    } catch (IOException e) {
        System.out.println("IO error in server thread");
    }
    String[] message = new String[] { "" };
    try {
        while (message[0] != "234124214") {
            message = (String[]) input.readObject();
            if (message[0] != null) {
                playerCoords.put(Integer.parseInt(message[0]), message[1]);

                output.writeObject(playerCoords);
                output.flush();
            } else {
                output.writeObject(Integer.toString(s.getPort()));
                output.flush();
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void closeWindow() throws IOException {
    input.close();
    output.close();
    s.close();
    System.out.println("Connection Closed");
}
}

标签: javamultithreadingserverlinkedhashmap

解决方案


推荐阅读