I'm developing a distributed system that receives requests via Socket and writes the results back using OutputStream. 

I'm facing problems because I have to return a very large string, but Java just keeps the execution of the code after I call the method print() without it finished printing: 

```
public void attendRequisition(Socket clientSocket) {
    PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());

    pw.print(getResults());

    pw.close()
}

```

Some information:

* I **need** to close the connection after sending the results.
* Without the close() instruction, the whole printing process takes about 3~5 seconds to finishes
* I'm using telnet to test this method, and I can see the message coming and being cut becase of Java running the close() instruction
* I **can't** count with receiving client's feedback

I believe this is happening because Java thinks the method print() finished executing, but what might be happening is that Java sends the message to the JVM, and JVM send it to the network adapter, and since the message is very large, close() method is called before the network adapter is able to send the entire message.

What I need is a way to make sure Java will only run the instruction close() after the printing process finished and the client received the entire message.

**UPDATE**: I was able to get it working properly by adding a `Thread.sleep()`. By doing this, the SO has enough time to send the message before Java closes the connection. But I don't want to use this solution, it implicates on too many problems.