I can send data to the client immediately after I receive it on the server, before closing the connection. How can I send data from the server to the client without waiting for this situation?
Once there is a connection, there is no longer any difference between the client and the server. Whatever you do with the client socket can also be done with the server socket.
BTW, you don't need to immediately close the connection. This was just an example.