'Writing and reading Bytes AND Objects between sockets without deprecated methods in Java

I am trying to make a web servers cluster in Java. The first thing a server does after it starts running is ask another one to join the cluster. In this process, a copy of the WebServer object itself is sent to the already running server, so that it is added to the list of running servers. Before and after the object is sent, text is also written and read between the two servers. I have managed to make that work, however only using the deprecated method readLine() of ObjectInputStream.

boolean requestJoin(String nextIP, int nextPort) {
    try (Socket serverSocket = new Socket(nextIP, nextPort)) {
        ObjectOutputStream objOut = new ObjectOutputStream(serverSocket.getOutputStream());
        objOut.writeBytes("JOIN\r\n\r\n");
        objOut.writeObject(this);
        
        ObjectInputStream objIn = new ObjectInputStream(serverSocket.getInputStream());

        String fromServer = objIn.readLine(); // Deprecated
        if (fromServer.equals("OK")) {
            System.out.println("Request to join cluster successful.");
        }
        else {
            System.out.println("Request to join cluster failed.");
            return false;
        }
        

        while (objIn.readLine() != null) {
            if (fromServer.startsWith("ID: ")) {
                int ID = Integer.parseInt(fromServer.substring(4));
                setID(ID);
            }
        }

        ServerList serverList = (ServerList) objIn.readObject();
        this.serverList = serverList;
        HashMap<String, String> fileList = (HashMap<String, String>) objIn.readObject();
        this.fileList = fileList;

        serverSocket.close();
        return true;
    }
    catch (IOException|NumberFormatException|ClassNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
// Is called after the first line of the request is read
boolean approveJoin() throws IOException {
    try {
        while (!objIn.readLine().equals(""));

        ObjectOutputStream objOut = new ObjectOutputStream(clientSocket.getOutputStream());
        WebServer server = (WebServer) objIn.readObject();

        String IP = server.getIP();
        int Port = server.getPort();

        int ID = serverList.size();
        server.setID(ID);
        serverList.add(server);
        objOut.writeBytes("OK\r\n");
        objOut.writeBytes("ID: " + ID + "\r\n\r\n");

        objOut.writeObject(serverList);
        objOut.writeObject(fileList);
        System.out.println("Approved join from " + IP + ":" + Port);

        return true;
    }
    catch (IOException|ClassNotFoundException e) {
        System.out.println("Join failed.");
        e.printStackTrace();
        String response = "NOT OK\r\n\r\n";
        outToClient.writeBytes(response + "\r\n\r\n");
        return false;
    }
}

Is there a different way to achieve this without using any deprecated methods?

Any other corrections/optimizations/suggestions are welcome.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source