1.14.4 Socket do not accept connection after disconnect

Discussion in 'Spigot Plugin Development' started by JoeyPlayzTV, Feb 6, 2020.

  1. I try to make an remote acp for my server with sockets.

    I have the folowing code


    Server:
    Code (Java):
    public static void main(String[] args) {

            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    for (ClientHandler clinet : clients) {
                        clinet.beat();
                        System.out.println("beated ");
                    }
                }
            }, 0, 5000);

            try {
                ServerSocket listener = new ServerSocket(4488);
                while (true) {
                    System.out.println("Warte auf Verbindung");
                    Socket client = listener.accept();
                    ClientHandler clienthandler = new ClientHandler(client);
                    clients.add(clienthandler);
                    clientpool.execute(clienthandler);
                    System.out.println("Verbunden ist: " + client.getInetAddress().getHostAddress());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    ClientHandler:
    Code (Java):
    public class ClientHandler implements Runnable {

        private Socket client;
        private BufferedReader in;
        private PrintWriter out;
        private String name;
        private boolean connected = true;

        public ClientHandler(Socket client) {
            this.client = client;
            try {
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                out = new PrintWriter(client.getOutputStream(), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public Socket getClient() {
            return client;
        }

        public String getName() {
            return name;
        }
       
        public void beat() {
            out.println("->heartbeat");
        }

        @Override
        public void run() {
            try {
                while(connected) {
                    out.println("heartbeat");
                    String fromclient = in.readLine();
                    if(fromclient != null) {
                        if(fromclient.equals("disconnect")) {
                            connected = false;
                            client.close();
                            JavaProgramm.clients.remove(this);
                            System.out.println("Disconnected -> " + JavaProgramm.clients.size());
                        }
                    }
                }
            } catch (Exception e) {
                connected = false;
                e.printStackTrace();
            }
        }
    }
    Client:
    Code (Java):
    public static void main(String[] args) throws UnknownHostException, IOException {
            Socket socket = null;
            PrintWriter out = null;
            ServerConnection conn = null;
            Thread serverlistener = null;
           
            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
           
            while(true) {
                String servercommad = console.readLine();
                if(servercommad.equals("exit")) {
                    running = false;
                    conn = null;
                    out.println("disconnect");
                    socket.close();
                    socket = null;
                }else if(servercommad.equals("connect")) {
                    socket = new Socket("127.0.0.1", 4488);
                    out = new PrintWriter(socket.getOutputStream(), true);
                    conn = new ServerConnection(socket);
                    serverlistener = new Thread(conn);
                    serverlistener.start();
                    System.out.println("Der client ist nun gejoint");
                }else {
                    out.println(servercommad);
                }
            }
        }
    ServerConnection (For reciving from Server)
    Code (Java):
    public class ServerConnection implements Runnable {
       
        Socket server;
        BufferedReader in;
       
        public ServerConnection(Socket server) {
            this.server = server;
            try {
                in = new BufferedReader(new InputStreamReader(server.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            try {
                while(Client.running) {
                    String fromserver = in.readLine();
                    if(fromserver != null) {
                        System.out.println("Server to client " + fromserver);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    My problem is, if i send the server the "disconnect" string the client on the server gets closed and the clinethandler removed. but if i write "connect" on the server connect after a connect to connect again the server acceps the connection but i can not send anymore. what i make wrong. my plan is

    Wirte connect on the client to connect and write disconnect to disconnect. How i can make a disconnect why dont work this?


    Thanks for help :(