I know that this question has been asked several times already. However, after following the advise in all the other questions I am still stuck as to what the problem might be.

I have a **Server** and a **Client**. A simple ping/pong program. After running the server and then the client and giving it some time to run its course, timeout exceptions start being thrown every now and then...

The timeout is there to prevent a block, however, if removed, it would cause the program to stall.

Is there a way to prevent this from occurring?

**Server.java**

```
public static void main(String args[]) {
        try {
            ServerSocket serverSocket = new ServerSocket(3000);
            while (true) {
                Socket socket = serverSocket.accept();
                String message = null;
                try {
                    socket.setSoTimeout(3000);
                    try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
                        message = (String) objectInputStream.readObject();
                        System.out.println("Server received " + message);
                    }
                    socket.close();
                } catch (IOException | ClassNotFoundException ex) {
                    //This exception is thrown because it hangs, but why does it hang?
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
                if ((message != null) && (message.equals("Ping"))) {
                    try {
                        Socket pongSocket = new Socket("localhost", 3000);
                        pongSocket.setSoTimeout(3000);
                        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(pongSocket.getOutputStream())) {
                            objectOutputStream.writeObject("Pong");
                            objectOutputStream.flush();
                            System.out.println("Server sent Pong");
                        }
                        pongSocket.close();
                        continue;
                    } catch (IOException ex) {
                        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

```

**Client.java**

```
public static void main(String args[]) {
        while (true) {
            try {
                Socket pingSocket = new Socket("localhost", 3000);
                pingSocket.setSoTimeout(3000);
                try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(pingSocket.getOutputStream())) {
                    objectOutputStream.writeObject("Ping");
                    System.out.println("Client sent Ping");
                    objectOutputStream.flush();
                }
                pingSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

```