我想从 udp 数据包中读取 1 位。
udp 数据包如下所示(c 代码):
struct typedef struct s_foo
{
__u32 foo;
__u8 bar:1,
bob:1;
} __attribute__((packed)) s_foo;
我如何在java中阅读这个?
到目前为止,我有这个,但我无法阅读 foo 或 bar 或 bob ......
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class BroadcastListener
{
public static void main(String[] args) throws IOException
{
if (args.length != 1) {
System.out.println("usage: <program> port");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
DatagramSocket dgram = new DatagramSocket(port);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dgram.receive(packet);
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ": "
+ msg);
packet.setLength(buffer.length);
}
}
}