简体   繁体   中英

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM