So far I've used this site whenever I encountered a problem and I've found solutions too, but this time I have no idea what's even happening.

I am working on a game that is based on a 1-vs-1-multiplayer-mode. So far i have created a server and my program with the client.

My server creates a new thread with a socket for every client that connects with the server and when the "New Game"-Button is pressed in the game, the thread searches for another thread that is looking for a new game right now and once it found him, creates a separate thread that sends a message to both threads to signal them that a game has started, which is then sent through their socket to the program which reacts accordingly.

Here is my code:

Thread:

```
    public void run() {

    try {

        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
        ServerNachricht inputLine, outputLine;
        LabyrinthProtocol prot = new LabyrinthProtocol();

        while (socket.isConnected()) {

```

ServerNachricht is a class that consists of a type(int), a sender(player) and a message(String).
When the thread gets a new game message, the protocol changes the players status-value to "searching", then looks if another "searching" player exists and then changes both players values to "playing" and returns a new ServerNachricht of type Kampfbeginn with the found player as sender.
After the protocol returns the outputLine, this is what the thread does:

```
                    if (outputLine.getArt() == ServerNachricht.KAMPFBEGINN) {
                    System.out.println(outputLine.getSender().getSname()+" ist da");    
                    server.kampfbeginn(this, outputLine.getSender());
                }

```

The sysout just verifies that the protocol has actually found another player and is printing that players name to be sure. So far, this has always worked.
Here are the parts that call for a new game in the server:

```
    public void kampfbeginn(LabyrinthThread t, Spieler gegner) {
    KampfThread kampf = null;
    System.out.println(gegner.getSname()+" anerkannt");
    for(int i = 0;i<threads.size();i++){
        if(threads.get(i)!=null){
            System.out.println(threads.get(i).getSpieler().getSname());
            if(threads.get(i).getSpieler().getSname().equals(gegner.getSname())){
                LabyrinthThread gegnert = threads.get(i);
                kampf = new KampfThread(t,gegnert);
                t.setKampf(kampf);
                gegnert.setKampf(kampf);
                break;
            }
        }
    }

```

This code searches through every existing thread (the server stores them in a vector) and checks if that threads connected player is the player returned by the protocol. When the thread was found, both threads are then given to a newly created thread that stores both of them while also storing that new thread in both threads.
The new thread even verifies the connection with two sysouts:

```
    public KampfThread(LabyrinthThread spieler1, LabyrinthThread spieler2) {
    super();
    this.spieler1 = spieler1;
    this.spieler2 = spieler2;
    System.out.println(spieler1.getSpieler().getSname() + "ist drin");
    System.out.println(spieler2.getSpieler().getSname() + "ist drin");

}

```

which I also get every time.
After both connections are established, that thread sends a message to both threads so that they will notify their programs to start:

```
        case(ServerNachricht.KAMPFBEGINN):
        spieler1.ThreadNachricht(new ServerNachricht(ServerNachricht.KAMPFBEGINN,spieler2.getSpieler(),""));
        spieler2.ThreadNachricht(new ServerNachricht(ServerNachricht.KAMPFBEGINN,spieler1.getSpieler(),""));
        break;

```

which calls this method in the threads:

```
    public void ThreadNachricht(ServerNachricht s) {

    if(socket.isConnected()) {
        try {
            out.writeObject(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

```

The strange thing is that this works absolutely perfect about 80% of the time (so both programs go into the "game started" mode) but sometimes it just works for one or even neither program and the server gets either a
Connection reset by peer
or a
Socket closed
error in

```
    public void ThreadNachricht(ServerNachricht s) {

    if(socket.isConnected()) {
        try {
            out.writeObject(s);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

```

in the out.writeObject(s); line. There is no line anywhere that closes anything (I've even taken out every single close() out of anywhere to make sure that nothing can interfere) and there seems to be no pattern at all to when it works and when it doesn't (and not working closes the servers and the programs clientsocket so the program is unable to work when that happens). Is there any way I can guarantee that my program works or is there any error I made? I am rather desperate because I couldn't even do major tests to find out a pattern since starting the program twice with exactly the same setup still causes it to work most of the time.

Edit: I literally just had a situation in which one player went into the new game mode while the other one stayed in the main menu (resulting in a Connection reset by peer: socket write error for the server) twice in a row before it worked the third time without any problems in the same run. So I searched with both players but only one went into the game screen (and the other one got the error). I then pressed back to go into the main menu and did the same again with the same result. When I tried for the third time, it worked and both players got into the game screen and started interacting with each other.