0

So I want the Server to send the Client a Message, and then the client to do something when the client receives the message, but the client doesn't receive the message or the server doesn't send the message. I don't know why. I made the chats in threads, but first of all, here the ServerClass:

public class CommandServer {

private ServerSocket server;
private Socket client;
private PrintWriter writer;
private BufferedReader reader;
public static List<Gamemode> cache = new LinkedList<>();

public CommandServer() {}

public void start(){
    try{
        server = new ServerSocket(3);
        System.out.println(Master.prefix + "Commander started!");

        client = server.accept();
        System.out.println(Master.prefix + "Watcher accepted!");

        writer = new PrintWriter(client.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

        client.setKeepAlive(true);

        Thread thread = new Thread(new Threader(reader));
        thread.start();

        startNew(Gamemode.BEISPIEL);
        System.out.print(Master.prefix + "Send StartCommand!");
    }catch(Exception ex){

    }
}

public void startNew(Gamemode gm){
    cache.add(gm);
    writer.write("STARTNEW => " + gm.toString());
    writer.flush();
}

}

And the thread for the Server:

public class Threader implements Runnable{

private BufferedReader reader;

public Threader(BufferedReader reader) {
    this.reader = reader;
}

@Override
public void run() {
    while(true){
        String s = null;
        try {
            while((s = reader.readLine()) != null){
                if(s.startsWith("STARTED")){
                    String uuid = s.split(" => ")[1];
                    if(Master.register.getRegisterStatus(UUID.fromString(uuid)) == RegisterStatus.NEW){
                        if(CommandServer.cache.contains(Master.register.getGamemode(UUID.fromString(uuid)))){
                            CommandServer.cache.remove(Master.register.getGamemode(UUID.fromString(uuid)));
                            Master.register.setRegistered(UUID.fromString(uuid));
                            Master.servers.add(Master.register.getGamemode(UUID.fromString(uuid)), Master.register.getPort(UUID.fromString(uuid)));
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

And now the Client:

public class Watcher {

private Socket client;
private PrintWriter writer;
private BufferedReader reader;
public static List<Thread> runningServers = new LinkedList<>();

public Watcher() {}

public void start(){
    try {
        client = new Socket("localhost", 3);
        System.out.println(Daemon.prefix + "Watcher started and connected!");
        writer = new PrintWriter(client.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

        client.setKeepAlive(true);

        Thread thread = new Thread(new Threader(reader));
        thread.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void started(String uuid){
    writer.write("STARTED => " + uuid);
    writer.flush();
}

}

And the Thread for the Client:

public class Threader implements Runnable{

private BufferedReader reader;

public Threader(BufferedReader reader) {
    this.reader = reader;
}

@Override
public void run() {
    try {
        boolean b = true;
        while (b == true) {
            String s = null;
            while ((s = reader.readLine()) != null) {
                if(s.startsWith("STARTNEW")){
                    System.out.println(Daemon.prefix + "Recieved!");
                    Gamemode gm = Gamemode.valueOf(s.split(" => ")[1]);
                    ThreadHandler handler = new ThreadHandler(gm);
                    Thread thread = new Thread(handler);
                    thread.start();
                    Watcher.runningServers.add(thread);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Why doesn't the client receive the the Message?

Donald_W
  • 1,773
  • 21
  • 35
RageSkilln
  • 19
  • 1
  • 6

1 Answers1

0

Try using 4-digit port number, e.g. '6789'. If it does not help, you should write Server and Client classes on your own which will help you fully understand the problem. For 1 client - 1 server communication I recommend simple example from here. For multiple client - 1 server communication check this out.

Community
  • 1
  • 1
Wojtek
  • 1,288
  • 11
  • 16