Parse a DatagramPacket after converting it to a byte array in Java

前端 未结 4 1251
日久生厌
日久生厌 2021-02-04 20:42

I am trying to parse a DatagramPacket that I will receive at a socket. I know the format of the packet I will receive, which is a DHCPREQUEST packet, but I don\'t think that rea

4条回答
  •  长发绾君心
    2021-02-04 21:39

    I'm a bit late to this, but there's a ByteBuffer class:

    ByteBuffer b = ByteBuffer.wrap(request.getData());
    
    byte opcode = b.get();
    byte hwtype = b.get();
    byte hw_addr_len = b.get();
    byte hops = b.get();
    int xid = b.getInt();
    short seconds = b.getShort();
    

    Or, if you only need a single field:

    ByteBuffer b = ByteBuffer.wrap(request.getData());
    int xid = b.getInt(4);
    

提交回复
热议问题