-1

Im trying to Send out a DatagramPacket on a MulticastSocket. This bit is working but when i try and get the info out of the header its not working.

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");



        }

This works but i just get all of it printed out

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");



        }

Can anyone see why this would not work thanks.

user207421
  • 305,947
  • 44
  • 307
  • 483
1ftw1
  • 656
  • 2
  • 14
  • 26

1 Answers1

0

The fourth byte of data is given by packet.getData()[3].

If that doesn't answer as clarified in your comments you will have to clarify further.

user207421
  • 305,947
  • 44
  • 307
  • 483