Search code examples
javaclientserverlan

Assiging IP address to a JButton in Java


I have a Server-Client program where I get clients IP address when they connect to my server system using this line of code:

//connecting to clients

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 conneted from :" + socket.getLocalAddress().getHostName();
                }
                finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }
    catch (IOException ex) {
        Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Now I need to assign it to a JButton, so that the Button is clicked, a small message goes to that client (From that IP Address).

How can I achieve this?


Solution

  • Based on what you are showing us, I cannot see the implementation or code for the Button in question (i.e. The button that will be sending the message to the IP.)

    However what I can tell you is how you can accomplish that. Simply assign the IP address before printing it out. Then from within your Buttons Action method, you can use the variable.

    For instance:

    UPDATED

    The only option I see to this is to create a variable that will track how many clients are connected and name the buttons according to this. Also just for testing purposes, name the button the Host name so you know which button is relevant to which button. See below:

    Create a Global Variable called "clientIP", as well as an Integer to tract the number of clients, as well as an ArrayList to store the client buttons required.

    //note that .getHostName returns a String represenation of the hostname from that IP
    String clientIP = "";
    int clientNumber = 0;
    ArrayList<JButton> buttons = new ArrayList<JButton>();
    

    Next assign the hostname when it is fetched within the try/catch:

    try{
        clientIP = socket.getLocalAddress().getHostName();
    
        //This will add a new Button to the ArrayList with the Text of the Button being the Clients Host Name
        buttons.add(new JButton(clientIP));
    
        System.out.println("Client conneted from :" + clientIP);
        displayButton(buttons.get(clientNumber);
        clientNumber++;
    }
    

    In terms of implementation of Actions you can then do the following:

    public void actionPerformed( ActionEvent e ){
        if ( e.getSource() == buttons.get(0) )
            //this line to assign the hostname to a variable so that we can use it to send the message
            String thisIP = buttons.get(0).getText();
            //now use this to send your message
        if ( e.getSource() == buttons.get(1) )
            String thisIP = buttons.get(1).getText();
            //now use this to send your message
        if ( e.getSource() == buttons.get(2) )
            String thisIP = buttons.get(2).getText();
            //now use this to send your message
        if ( e.getSource() == buttons.get(3) )
            String thisIP = buttons.get(3).getText();
            //now use this to send your message
    }
    

    In order to display the buttons, you can simply have a method created that will print out the Button at that position everytime it is created (From with your Try statement)

    public static void displayButton(JButton newButton){
        // this line is to display the button on your JFrame. Edit the "frame" according to what the name of your JFrame is.
        frame.add(newButton);
    }
    

    Hope this answers your question!

    Let me know of the outcome.