简体   繁体   English

检测传入请求的数据类型

[英]Detect data type of incoming request

I'm using a objectoutputstream to send and receive data for known devices, but it might be possible in my case that also other requests with "String"-Streams might on the InputStream of the socket. 我正在使用objectoutputstream来发送和接收已知设备的数据,但是在我的情况下,套接字“ InputStream”上的其他带有“ String” -Streams的请求也可能会发生。

What is the best way to detect if the incoming stream can be handled by an ObjectInputStream or needs to be red over a normal bufferedreader/inputreader. 检测传入流是否可以由ObjectInputStream处理或需要在正常的bufferedreader / inputreader上变为红色的最佳方法是什么。

Thanks for your input! 感谢您的输入!

You can send a string as an object through a objectoutputsteam and on the other side you use instanceof to check if it is a string.If its not, you can keep checking using instanceof to find what class the object sent through is. 您可以通过objectoutputsteam将字符串作为对象发送,另一方面,您可以使用instanceof来检查它是否为字符串。如果不是,则可以继续使用instanceof进行检查以查找对象通过的类。 Once you find the right Class you have to cast the object to that class. 一旦找到正确的类,就必须将对象投射到该类。

After long back'n forth we decided to change the protocol. 经过很长一段时间后,我们决定更改协议。 As mentioned by @user8245293 is actually a bit messy. 如@ user8245293所述,实际上有点混乱。 It poses a security problem and would implement a quirky solution by detecting the stream type. 它带来了安全问题,并且将通过检测流类型来实现古怪的解决方案。

For us the best solution seemed to be - if others are interested - to do introduce light handshake protocol with some header information to arrange the stream type. 对于我们来说,最好的解决方案似乎是-如果其他人有兴趣-引入带有一些标头信息的轻型握手协议来安排流类型。

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

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