Here is a look at sending a simple DHCPDISCOVER Message:
First we want to create a DHCPSocket and open it on the DHCP client
port, 68:
DHCPSocket mySocket = new DHCPSocket(68); // create socketNow we will create a DHCPMessage object:
DHCPMessage messageOut = new DHCPMessage();This will create an empty DHCP Message to be sent to the local broadcast address (255.255.255.255) and to a server on port 67.
// fill DHCPMessage object
messageOut.setOp((byte) 1);
messageOut.setHtype((byte) 1);
messageOut.setHlen((byte) 6);
messageOut.setHops((byte) 0);
messageOut.setXid(ranXid.nextInt()); // should be a random int
messageOut.setSecs((short) 0);
messageOut.setFlags((short) 0);
byte[] hw = new byte[16];
hw[0] = (byte) 0x00;
hw[1] = (byte) 0x60;
hw[2] = (byte) 0x97;
hw[3] = (byte) 0xC6;
hw[4] = (byte) 0x76;
hw[5] = (byte) 0x64;
messageOut.setChaddr(hw);
// set message type option to DHCPDISCOVER
byte[] opt = new byte[1];
opt[0] = (byte) 1;
messageOut.setOptions(53, opt.length, opt);
Now we should be ready to send our DHCPDISCOVER Message to the server:
mySocket.send(messageOut); // send DHCPDISCOVERAnd receive our response from the server into another DHCPMessage object:
mySocket.receive(messageIn);Try this:
messageIn.printMessage();