简体   繁体   English

DataInputStream over DatagramSocket

[英]DataInputStream over DatagramSocket

I have wrote a generic program that can accept data from DataInputStream. 我写了一个可以接受DataInputStream数据的通用程序。 But recently I was trying to receive data from UDP using DatagramSocket. 但最近我试图使用DatagramSocket从UDP接收数据。 I have searched a lot, but I could nor fina a way to manipulate the incoming data from DatagramSocket to DataInputStream. 我已经搜索了很多,但我也可以通过一种方式来处理从DatagramSocket到DataInputStream的传入数据。 Logically, since both are incoming data, there should be a way to integrate these two objects right? 从逻辑上讲,由于两者都是传入数据,应该有一种方法来整合这两个对象吗? Am I wrong? 我错了吗?

After getting answered from EJP I am right now using like this am i right? 在得到EJP的回答之后我现在就像这样使用我是对的吗?

byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()));
While(true){
    dsocket.receive(packet);
    dis = new DataInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()));
    reader = new MAVLinkReader(dis,MAVLinkMessage.MAVPROT_PACKET_START_V10);
    mesg = reader.getNextMessage();
    while (mesg != null) {...do stuff...}
}

Now what i feel is that since the reader is initialized to new dis each time the previously remaining bytes in dis becomes vanished. 现在我的感觉是,因为每当前面剩下的字节消失时,读者就被初始化为新的。

Easy. 简单。

DataInputStream din = new DataInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength());

where packet is a DatagramPacket . packetDatagramPacket

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

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