Socket automatically disconnects

0

Hello,

I was making a connection from a Socket to a ServerSocket, but after the first message the Socket receives it it automatically disconnects.

Socket, read messages:

private void startRunnable() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (started) {
                try {
                    Object[] i = checkInput();
                    if ((boolean) i[1] == true) {
                        callEvent(ClientEvents.onReciveMessageFromServer,
                                new onClientReciveMessageFromServer(client, (String) i[0]));
                    }else{
                        client.shutdownInput();
                        client.close();
                        callEvent(ClientEvents.onEndConnection, new onClientEndConnection());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }).start();
    loaded = true;
}

Object[] checkInput() {
    try {
        String i = readInput();
        if (i != null) {
            return new Object[] { i, true };
        }
    } catch (Exception e) {
        return new Object[] { null, false };
    }
    return new Object[] { null, false };
}

private String readInput() throws Exception {
    try {
        DataInputStream dis = new DataInputStream(client.getInputStream());
        int length = dis.readInt();
        byte[] data = new byte[length];
        dis.readFully(data);
        String input = new String(data, "UTF-8");
        dis.close();
        return input;
    } catch (Exception e) {
        return null;
    }
}

ServerSocket, accept Sockets:

void startRunnables() throws Exception {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (started) {
                try {
                    Socket socket = server.accept();
                    new Thread(new ClientThread(socket, instance)).start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    this.loaded = true;
    callEvent(ServerEvents.onServerLoad, new onServerLoad(server, port));
}

ServerSocket, Client Thread:

public void run() {

    do {
        try {
            if (!server.connectedSockets.contains(socket)) {
                Object[] i = server.checkInput(socket);
                if (((boolean) i[1]) == true) {
                    if ((server.passwordEnabled ? ((String) (i[0])).equals(server.password) : true)) {
                        ServerClient sc;
                        server.connectedClients
                                .add(sc = new ServerClient(socket, socket.getInetAddress().getHostAddress()));
                        server.connectedSockets.add(socket);
                        server.callEvent(ServerEvents.onServerClientConnect, new onServerClientConnect(sc));
                    } else {
                        server.connectedSockets.add(socket);
                        server.callEvent(ServerEvents.onServerSocketConnect, new onServerSocketConnect(socket));
                    }
                } else {
                    server.kickSocket(socket);
                }
            }
            Object[] ci;
            if ((boolean) (ci = server.checkInput(socket))[1] == true) {
                if (server.connectedClients.contains(server.socketToClient(socket))) {
                    ServerClient client;
                    if ((client = server.socketToClient(socket)) != null) {
                        server.callEvent(ServerEvents.onServerReciveMessageFromClient,
                                new onServerReciveMessageFromClient(client, (String) ci[0]));
                    }
                } else {
                    server.callEvent(ServerEvents.onServerReciveMessageFromUnauthenticatedSocket,
                            new onServerReciveMessageFromUnauthenticatedSocket(socket, (String) ci[0]));
                }
            } else {
                if (server.connectedClients.contains(server.socketToClient(socket))) {
                    ServerClient client;
                    if ((client = server.socketToClient(socket)) != null) {
                        server.callEvent(ServerEvents.onServerClientLeave, new onServerClientLeave(client));
                        server.kickClient(client);
                    }
                } else {
                    server.callEvent(ServerEvents.onServerSocketLeave, new onServerSocketLeave(socket));
                    server.kickSocket(socket);
                }
            }
        } catch (Exception e) {
            return;
        }
    } while (server.connectedSockets.contains(socket));

}

Sorry if I did something very ugly, but it's my first time messing with Socket and ServerSocket

    
asked by anonymous 25.05.2018 / 00:25

0 answers