简体   繁体   English

是否可以通过套接字在同一流中发送主类型和字节数组

[英]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. 我正在使用tcp套接字在Java中构建一个远程桌面应用程序。 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. 我在套接字上创建一个PrintWriter / Scanner流,以在客户端和服务器之间交换鼠标,键盘和屏幕分辨率(主要类型)的命令,它工作良好。 After that i create a DataOutputStream/DataInputStream on same socket to transfer screen shot(byte array) from client to server then my app cashed. 之后,我在同一套接字上创建一个DataOutputStream / DataInputStream,以将屏幕快照(字节数组)从客户端传输到服务器,然后我的应用程序兑现了。

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. 如果您的意思是写原始类型的是DataOutputStream ,而不是BufferedWriter. In this case you should use DataOutputStream for everything. 在这种情况下,您应该对所有内容使用DataOutputStream 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. 通过BufferedWriter将东西转换为电线的字符串是没有意义的,只需要在另一端使用Scanner.再次解析它们即可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 . 参见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. 您可以将PrintWriter的内容写入ByteArrayOutputStream,然后将其内容作为字节数组写入DataOutputStream。 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. 在客户端,您读取了第一个字节数组,并以ByteArrayInputStream对其进行扫描,然后读取了第二个字节数组,这将是您的屏幕截图。 I guess you need to write the size of the byte arrays to the DataOutputStream before the arrays as well. 我想您还需要在数组之前将字节数组的大小写入DataOutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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