Java - TCP socket connection not working

I am using TCP connection to send data back and forth between a client and a server program. In the code below for the server I have set up the server and have a listen method to listen for incoming connections.

private void initialiseServer() {
    try {
        serverSocket = new ServerSocket(22222, 8, InetAddress.getLocalHost());
        System.out.println("Hosting server");
    } catch (Exception e) {
        e.printStackTrace();
    }
    playerone = false;
}

private void listenForServerRequest() {
    try {
        socket = serverSocket.accept();
        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());
        accepted = true;
        System.out.println("CLIENT HAS REQUESTED TO JOIN, ACCEPTED");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

When a connection is made the server should output into its console "CLIENT HAS REQUESTED TO JOIN, ACCEPTED".

Below is the client socket program:

private boolean connect(String ip) {
    try {
        socket = new Socket(ip, 22222);
        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());
        accepted = true;
    } catch (IOException e) {
        System.out.println("Unnable to connect to address");
        return false;
    }
    System.out.println("Successfully connected");
    return true;
}

Which should output "Successfully connected" when connected.

This program works when I use a single computer running both client and server connecting to itself via "localhost" as the IP but when attempting to connect to other computers using the same internet router the client just pauses for about 30 seconds trying to connects and then fails.

To determine the IP of the host computer in which the client computer puts into the connect method I use the ipv4 but I am not sure as to whether this is the correct address with which to test with or if there is something else I have to do in which to connect to someone via sockets. Unfortunately as I do not have 2 devices readily available I am only able to test it every so often thus making it more difficult to find a solution. Any advice would be greatly appreciated!