Connect multiple clients to one server
<p>I'm trying to connect all my clients to one server. I've done some research and found out that the easiest way to do it is create a new thread for every client that connects to the server. But I am already stuck on the part where a client disconnects and reconnect.</p>

<p>Client</p>

<pre><code>import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Test {
    private static int port = 40021;
    private static String ip = "localhost";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        String command, temp;
        Scanner scanner = new Scanner(System.in);
        Socket s = new Socket(ip, port);
        while (true) {
            Scanner scanneri = new Scanner(s.getInputStream());
            System.out.println("Enter any command");
            command = scanner.nextLine();
            PrintStream p = new PrintStream(s.getOutputStream());
            p.println(command);
            temp = scanneri.nextLine();
            System.out.println(temp);
        }
    }

}
</code></pre>

<p>Server</p>

<pre><code>import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class MainClass {

    public static void main(String args[]) throws IOException {
        String command, temp;
        ServerSocket s1 = new ServerSocket(40021);
        while (true) {
            Socket ss = s1.accept();
            Scanner sc = new Scanner(ss.getInputStream());
            while (sc.hasNextLine()) {
                command = sc.nextLine();
                temp = command + " this is what you said.";
                PrintStream p = new PrintStream(ss.getOutputStream());
                p.println(temp);
            }
        }
    }
}
</code></pre>

<p>When I connect once it works correctly but as soon as I disconnect the client and try to reconnect (or connect a second client) it does not give an error or anything it just does not function. I am trying to keep it as basic as possible.</p>

<p>The output with one client:
<a href="https://i.stack.imgur.com/GKLPR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GKLPR.png" alt="Correct output"></a></p>

<p>When I try and connect a second client: 
<a href="https://i.stack.imgur.com/s3YJZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s3YJZ.png" alt="Output with 2 clients connected"></a></p>

<p>I hope someone could help me out. Thanks in advance.</p>