Java Chat无法正常工作 - 客户端无法接收消息

时间:2016-09-24 09:25:30

标签: java multithreading networking

所以我希望服务器向客户端发送消息,然后客户端在客户端收到消息时执行某些操作,但客户端没有收到消息,或者服务器没有发送消息信息。我不知道为什么。我在线程中进行了聊天,但首先,这里是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();
}

}

服务器的线程:

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();
        }
    }
}

}

现在是客户:

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();
}

}

客户线程:

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();
    }
}

}

为什么客户端没有收到消息?

1 个答案:

答案 0 :(得分:0)

尝试使用4位数的端口号,例如'6789'。如果它没有帮助,您应该自己编写Server和Client类,这将有助于您完全理解该问题。对于1个客户端 - 1个服务器通信,我建议使用here中的简单示例。对于多个客户端 - 1服务器通信check this out