JDHCP Package mini Tutorial



We wrote the JDHCP API with the intent of making it easier to write DHCP applications, in Java(tm), easier. Currently we are using this package to develop an administrative application for DHCP. The creation of this API has made the coding of this application very easy.

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 socket
Now 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.
Now we will fill our message with the values needed to send a DHCPDISCOVER Message, read rfc2131 to learn what these values mean:
            // 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 DHCPDISCOVER
And receive our response from the server into another DHCPMessage object:
mySocket.receive(messageIn);
Try this:
messageIn.printMessage();

Questions? email us.

Last modified: 11/19/98