简体   繁体   English

Java聊天无法正常工作-客户端未收到消息

[英]Java Chat not working - Client does not receive the message

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: 我在线程中进行了聊天,但首先是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? 客户为什么没有收到消息?

Try using 4-digit port number, eg '6789'. 尝试使用4位数字的端口号,例如'6789'。 If it does not help, you should write Server and Client classes on your own which will help you fully understand the problem. 如果没有帮助,您应该自己编写Server和Client类,这将帮助您完全理解问题。 For 1 client - 1 server communication I recommend simple example from here . 对于1客户端1服务器通信,我从这里推荐一个简单的示例。 For multiple client - 1 server communication check this out . 对于多客户端1服务器通信, 请检查此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM