Writing Text

PrintWriter out =
											new PrintWriter(socket.getOutputStream(), true);
											out.print(msg);
											out.flush();

This phrase assumes that we’ve previously created a socket to the server from which we want to write text. See the phrase “Contacting a Server” in this chapter for more details on creating the socket instance. Given the socket instance, we call the getOutputStream() method to get a reference to the socket’s output stream. With that, we instantiate a PrintWriter for writing text across the network to the server with which we are connected. The second parameter that we pass to the PrintWriter constructor in this phrase sets the auto-flush option. Setting this to true will cause the println(), printf(), and format() methods to automatically ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.