一个面向所有客户的主要LinkedHashMap

时间:2018-08-29 08:04:16

标签: java multithreading server 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");
}
}

1 个答案:

答案 0 :(得分:0)

这可以通过将playerCoords地图声明为静态来解决。因此,ServerThread类的所有实例都可以访问相同的LinkedHashMap。

您的代码似乎是游戏的开始。我不会将此地图保留在播放器连接类内。我会将其保留在其他位置,例如您的“ Main”类。

编辑:

通过使用这两种方法,您将需要同步此映射,否则可能会有ConcurrentModificationException。您应该使用同步的方法/块或Collections.synchronizedMap

创建自己的写/读逻辑
相关问题