Christopher Francisco
Christopher Francisco

Reputation: 16288

Java socket write byte[] instead of String

I'm new to socket programming, and I've been following several tutorials like this one http://www.myandroidsolutions.com/2012/07/20/android-tcp-connection-tutorial/ and was able to build a client-server architecture over TCP using socket.

The thing is, the tutorials are basic so they teaches you how to send using PrintWriter and BufferedReader which seems to write and read String. I have to encrypt my data and send it as byte[], so the question will be:

  1. For this mean, can I still use PrintWriter and BufferedReader? if not then
  2. What classes should I use for this means and how?, I have seen DataOutputStream and DataInputStream and several others, but have not found their difference or why use one or another.

PS: The encryption part is already done, so don't worry about it as it is not the question :)

Upvotes: 1

Views: 473

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503679

You should use an OutputStream to write and an InputStream to read. Those are for binary data - anything with a suffix of Writer or Reader is for text data.

You may find DataOutputStream and DataInputStream useful - they basically add some services wrapped around a vanilla OutputStream. But if all you need to do is write a byte[] to a stream, then plain OutputStream is fine.

Upvotes: 7

Related Questions