0% found this document useful (0 votes)
12 views

Network Programming Section 9

Uploaded by

basma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Network Programming Section 9

Uploaded by

basma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Prepared by: Eng/ Basma Elshoky

Section 9: DHCP

DHCP stands for Dynamic Host Configuration Protocol. It's a network management
protocol used to automatically assign IP addresses and other network configuration
parameters to devices on a network. DHCP operates on the client-server model
where a DHCP server dynamically assigns IP addresses and related configuration
information to DHCP clients.

DHCP Components
- DHCP Server: Distributes IP addresses and configuration options.
- DHCP Client: Requests an IP address and configuration from the server.

DHCP Process
1. DHCP Discovery: When a device connects to a network, it sends out a broadcast
message called DHCP Discover to find a DHCP server. "Client broadcasts a DHCP
Discover message to find a server."
2. DHCP Offer: The DHCP server responds with a DHCP Offer message, offering
an IP address and other configuration parameters." Server sends a DHCP Offer
message containing a proposed IP address and lease time."

3. DHCP Request: The client selects one of the offered IP addresses and sends a
DHCP Request message to the DHCP server." Client broadcasts a DHCP Request
message to accept the offered IP."
4. DHCP Acknowledgment: The DHCP server acknowledges the request by
sending a DHCP Ack message, confirming the IP address lease and providing
additional configuration details." Server sends a DHCP Acknowledgement message
confirming the lease."
5. IP Lease: The client uses the assigned IP address and network configuration for a
specific lease duration.
Prepared by: Eng/ Basma Elshoky

6. Release (Optional): Client sends a DHCP Release message when finished with
the IP.

Example: Implementing a Simple DHCP Client

import java.io.*;
import java.net.*;
public class DHCPClient {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
socket.setSoTimeout(5000); // Set timeout to 5 seconds

// Prepare DHCP Discover packet


byte[] discoverData = {0x01, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
InetAddress serverAddress = InetAddress.getByName("255.255.255.255");
int serverPort = 67;

DatagramPacket discoverPacket = new DatagramPacket(discoverData,


discoverData.length, serverAddress, serverPort);

// Send DHCP Discover packet


Prepared by: Eng/ Basma Elshoky

socket.send(discoverPacket);

// Receive DHCP Offer packet


byte[] offerData = new byte[1024];
DatagramPacket offerPacket = new DatagramPacket(offerData,
offerData.length);
socket.receive(offerPacket);

// Process DHCP Offer packet


// You can parse and extract IP address and other configuration details here

System.out.println("Received DHCP Offer from server.");

} catch (SocketTimeoutException e) {
System.err.println("No response from DHCP server.");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}
Prepared by: Eng/ Basma Elshoky

Explanation:
- This Java code demonstrates a simple DHCP client that sends a DHCP Discover
packet to find a DHCP server and waits for a DHCP Offer response.
- It sends the Discover packet as a broadcast message to the network's broadcast
address. The data 0x01 is represented in hexadecimal format. In Java, it's treated as
an integer literal. In this context, it's likely used to represent a specific type of
message or option in the DHCP protocol. In DHCP, message types are represented
by specific codes, and 0x01 typically represents a DHCP Discover message type.
DHCP messages consist of a header followed by a variable-length options field. The
first byte of the options field typically represents the message type, where 0x01
corresponds to a Discover message.
- After sending the Discover packet, it waits for a response from the DHCP server
for up to 5 seconds. If no response is received within this time, it times out.
- The received DHCP Offer packet can be further processed to extract IP address and
other configuration details.

Notes:
The program will execute and attempt to send a DHCP Discover packet to find a
DHCP server on the network. If a DHCP Offer is received within the timeout period,
it will print "Received DHCP Offer from server.". If no response is received within
the timeout, it will print "No response from DHCP server."
We have not appropriate permissions to send and receive UDP packets on your
network. DHCP server not available on our network to respond to the DHCP
Discover packet sent by the client.

You might also like