How can I connect multiple users together and send each connected user information? (Java) [closed]

I am making an (Android) app with Java that requires me to create rooms that one person can host and then other users could join, and then make all the users do something at the same time.

My socket server knowledge is very limited, so my theory would be that a server is opened when a user decides to host a room, and then other users can connect to this server, and then every connected user gets sent the same information. Could someone point me to the right direction, I am coding this (Android) application in Java by the way, it would be much appreciated. I am quite new to all this so please be indulgent.

Answer

Your problem is very complex.
If you use TCP protocol you have to use more Thread.
When a client choose to be the host, it will open a ServerSocket on an IpAddress and on a Port. You can check the javaDoc for details.
At this point this client must create more Thread (using java.lang.Thread or java.lang.Runnable).
Every time that another client want to join the room, it will be on another Thread.
You can use a Monitor for share data. For example:

try {

        ServerSocket serverSocket = new ServerSocket(PORT);

        while (isOpen) {
            if (serverSocket != null) {
                Socket client= serverSocket.accept();
                new ComunicateClientThread(client, monitor).start();
            }
        }

    } catch (EOFException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

Using the TCP protocol, every client must stay connected all the time.
When a client choose to enter on an existing room, he have to know Ip and Port of the host. It will create a Socket and so can start the comunication using the Stream. For example:

try {
        Socket socket = new Socket(address, port);
        ObjectInputStream inputData = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream outputData = new ObjectOutputStream(socket.getOutputStream());

    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

I hope i was clear. Let me know if you have others problems and sorry for my bad english