Search code examples
javamultithreadingserverclientclient-server

How can a Java thread object call a method from the original class?


I have a client-server chat program, where the server will create a a new thread for each accepted connection, where NewClient extends Thread to handle requests from a specific client.

Here is a snippet of the server code:

public void startServer() {
    // thread to handle requests
    new Thread(() -> {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("Server started at " + new Date());

            while (true) {
                // listen for connection request
                Socket socket = serverSocket.accept();

                // increment total clients connected
                clientCount++;

                // create new client
                NewClient newClient = new NewClient(socket, clientCount);

                // add client to active client list
                activeClientList.add(newClient);

                // start thread
                newClient.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();
}

In NewClient, the thread will loop and check the input stream. If an input stream is detected, I want it to broadcast the message in the input stream to all clients.

Here is the run method of NewClient:

public void run() {
    while(true) {
        try {
            // read from client
            Data message = (Data) objectInputStream.readObject();
            System.out.println("Received " + message.text + " from " + message.name);

            // broadcast to all clients
            // broadcastMessage(message)


        } catch (IOException | ClassNotFoundException e) {
            System.out.println("Failed to get input stream");
            e.printStackTrace();
        }
    }
}

Back in the server class, there is a method called broadcastMessage(Data data), which will loop through all connected clients in an array list and use the the output streams to send the data to each client.

Here is the method:

public synchronized void broadcastMessage(Data data) {
    Integer size = activeClientList.size();

    // loop through all clients
    for(int i=size-1;i>=0;i--) {
        NewClient client = activeClientList.get(i);

        if(!client.writeToClient(data)) {
            activeClientList.remove(i);

            // notify users of disconnect
            Data update = new Data(data.name, data.name + " has disconnected.");
            broadcastMessage(update);
        }
    }
}

I understand that if this was an anonymous thread, I can call the broadcast method from within the server. How can I call this method within a thread class?

Thank you in advanced for your feedback.


Solution

  • // create new client
    NewClient newClient = new NewClient(socket, clientCount, this);
    

    Thank you Johannes Kuhn for pointing out that the server can be passed into the thread constructor to call the method.