使用java中的JButton向客户端发送消息

时间:2015-04-19 20:47:35

标签: java swing client server

我有一个Java程序,只要新客户端连接到我的服务器程序,就会生成运行时JButtons。只有在单击为该特定客户端分配的JButton时,我才需要向特定客户端发送消息。

void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton2.setText("Server Running!");
            jButton2.setEnabled(false);
            try {
                while (true) {

                    socket = listener.accept();
                    try {

                           //  System.out.println("Client connected from " + socket.getLocalAddress().getHostName());
                            clientIP= socket.getLocalAddress().getHostName();
                            buttons.add(new JButton(clientIP));
                           displayButton(buttons.get(clientNumber),clientNumber);
                            clientNumber++;

                 } 
                    finally {
                        socket.close();
                    }
                }
            }
            finally {
                listener.close();
            }
        }
        catch (IOException ex) {
            Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

我试过这样的事情。

public void actionPerformed( ActionEvent e ) throws IOException{
    if ( e.getSource() == buttons.get(1) )
    {
        String thisIP = buttons.get(1).getText();
        PrintWriter out =
                        new PrintWriter(socket.getOutputStream(), true);

                         out.println("hey");
                         JOptionPane.showMessageDialog(null,"gfb");
    }
  }

这些是经历过的:

String clientIP = "";
    int clientNumber = 0;
    ArrayList<JButton> buttons = new ArrayList<JButton>();
    Socket socket;

1 个答案:

答案 0 :(得分:0)

这是您的代码修改为使用TreeMap(已排序的HashMap)而不是ArrayList,其中每个按钮都映射到套接字对象,请注意套接字不再是全局的。如果有什么不清楚,请告诉我。

  int clientNumber = 0;
  TreeMap buttonsMap = new TreeMap();
  buttonListener blistener = new buttonListener();
  void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton2.setText("Server Running!");
            jButton2.setEnabled(false);
            try {
                while (true) {

                    Socket socket = listener.accept();
                    try {

                         //  System.out.println("Client connected from " + socket.getLocalAddress().getHostName());
                         String clientIP= socket.getLocalAddress().getHostName();
                         JButton button = new JButton(clientIP);
                        button.addActionListener(blistner);
                         buttonsMap.put(button, socket);
                         displayButton(button,clientNumber);
                         clientNumber++;

                 } 
                 catch(Exception e)
                 {
                 }
               }
            }
            finally {
                listener.close();
            }
        }
        catch (IOException ex) {
            Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



private class buttonListener implements ActionListener
  {  
    public void actionPerformed( ActionEvent e ) throws IOException{
        if ( JButton button= e.getSource())
        {
            Socket socket = (Socket)buttonsMap.get(button);
            PrintWriter out =
                            new PrintWriter(socket.getOutputStream(), true);

                             out.println("hey");
                             JOptionPane.showMessageDialog(null,"gfb");
        }
      }
    }