简体   繁体   中英

Is it posible to send primary type and byte array in same stream over socket

My broblem is:

I'm building a remote desktop application in java using tcp socket. I create a PrintWriter/Scanner stream on socket to exchange command of mouse, keyboard and screen resolution(primary type) between client and server, it work good. After that i create a DataOutputStream/DataInputStream on same socket to transfer screen shot(byte array) from client to server then my app cashed.

Can anyone help me to resolve this broblem, many thank!

I don't know what you mean by 'primary type'. It's DataOutputStream that writes primitive types, if that's what you mean, not BufferedWriter. In this case you should use DataOutputStream for everything. There's no sense in converting things to strings for the wire via BufferedWriter only to have to parse them again at the other end with a Scanner. There's also a space cost. And finally you can't do it anyway because of the buffering issues.

The same socket should not be used this way. See https://stackoverflow.com/a/18449544/337621 . You could write the content of the PrintWriter into a ByteArrayOutputStream, and then you can write the content of it into the DataOutputStream as byte array. On client side you read the first byte array, you do the scanning on it as ByteArrayInputStream, then you read the second byte array, which will be your screenshot. I guess you need to write the size of the byte arrays to the DataOutputStream before the arrays as well.

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