我试图在 MulticastSocket 上发送一个 DatagramPacket。这一点是有效的,但是当我尝试从标题中获取信息时它不起作用。
while(true){
byte[] packet = new byte[1500];
DatagramPacket packetR = new DatagramPacket(packet, packet.length);
socketM.receive(packetR);
ByteBuffer data = ByteBuffer.wrap(packetR.getData());
byte[] senderAddress = new byte[100];
byte[] senderCommand =new byte[100];
data.get(senderAddress,0,4);
InetAddress senderIP = InetAddress.getByAddress(senderAddress);
data.position(4);
data.get(senderCommand,0,1);
String command = (char)senderCommand[0]+"";
System.out.print(senderIP+": "+ command+"\n");
}
这行得通,但我只是把它全部打印出来
while(true){
byte[] packet = new byte[100];
DatagramPacket packetR = new DatagramPacket(packet, packet.length);
socketM.receive(packetR);
InetAddress ip = packetR.getAddress();
String meg = new String(packetR.getData());
System.out.print(ip+": "+ meg+"\n");
}
谁能明白为什么这不起作用谢谢。