Writing Binary Data

DataOutputStream out =
											new DataOutputStream(socket.getOutputStream());
											out.write(byteArray, 0, 10);

In a previous phrase in this chapter, “Writing Text,” we showed how to write text data across a network. In this phrase, we show how to write binary data across a network. 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.

In this phrase, we call the getOutputStream() method of the socket instance to obtain a reference to the socket’s output stream. We then instantiate a DataOutputStream, which we can use to write binary data across the network. We use the write() method ...

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.