Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] - Handling more than one connection #32

Closed
kappsegla opened this issue Feb 6, 2024 · 0 comments · Fixed by #33
Closed

[FEATURE] - Handling more than one connection #32

kappsegla opened this issue Feb 6, 2024 · 0 comments · Fixed by #33
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@kappsegla
Copy link
Contributor

kappsegla commented Feb 6, 2024

Problem Description

At the moment our code in clientHandler.handleConnections() isn't using threads or async I/O and can therefore not handle multiple clients at the same time. Can be seen as some sort of a bug and will have impacts on future developement.

I propose moving the call to serverSocket.accept() into our start method in Server class and then call ClientHandler handleConnection with the retrieved Socket objekt in a new thread.

Since we are using Java21 i propose using virtual threads.
Code suggestions:

public void start() {
        try {
            System.out.println("Server started on port 1883");
            while (!serverSocket.isClosed()) {
                Socket connection = null;
                connection = serverSocket.accept();
                Thread.ofVirtual(() -> clientHandler.handleConnection(connection));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@kappsegla kappsegla added bug Something isn't working enhancement New feature or request labels Feb 6, 2024
@vonfredd vonfredd self-assigned this Feb 6, 2024
@vonfredd vonfredd linked a pull request Feb 6, 2024 that will close this issue
@kappsegla kappsegla added this to the Connection handling milestone Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants