DHCP Mode of Ethernet Library doesn't works while Static works?

Hello!! Everyone!!

Most of the peoples had faced this problem, but i haven’t found any solution for it.

I am trying to connect the Ethernet Shield connected with Arduino Mega to my PC, through Ethernet.

The code is as follow:-

/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

#define ETH_CS    10
#define INT_SD_CS  4

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0x90,0xA2,0xDA,0x0E,0xCD,0x1D};

void setup()
{
  SelectEthernetModule();
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // this check is only needed on the Leonardo:
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("DHCP Started..");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
    ;
  }
  
  // print your local IP address:
  Serial.print("My IP address: ");
  
  for (byte thisByte = 0; thisByte < 4; thisByte++)
  {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop()
{

}

void SelectEthernetModule(void)
{
  // Select the Ethernet Module and De-select the Internal SD Card Module.
  digitalWrite(ETH_CS,LOW);			// Select the Ethernet Module.
  digitalWrite(INT_SD_CS,HIGH);		// De-Select the internal SD Card
}
void DeSelectEthernetModule(void)
{
  // DeSelect both, Internal SD and Ethernet Module
  digitalWrite(ETH_CS,HIGH);
  digitalWrite(ETH_CS,HIGH);
}

I don’t know why i am always getting this error.

Failed to configure Ethernet using DHCP

Apart from that when i tried to connect with static mode, it works properly.
In Static mode, i assigned IP 192.168.1.17 to Ethernet Shield and set IP address of my PC as 192.168.1.10 acting as server, subnet mask 255.255.255.0 and Gateway as 192.168.1.1

The code for static connection is as follow:-

#include <SPI.h>
#include <Ethernet.h>

#define ETH_CS    10
#define INT_SD_CS  4

IPAddress ip(192,168,1,17);
// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,10); 

EthernetClient client;

void setup()
{
  SelectEthernetModule();
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial)
   {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 5000))
  {
    Serial.println("connected");
  } 
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    client.write(c);
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

void SelectEthernetModule(void)
{
  // Select the Ethernet Module and De-select the Internal SD Card Module.
  digitalWrite(ETH_CS,LOW);			// Select the Ethernet Module.
  digitalWrite(INT_SD_CS,HIGH);		// De-Select the internal SD Card
}
void DeSelectEthernetModule(void)
{
  // DeSelect both, Internal SD and Ethernet Module
  digitalWrite(ETH_CS,HIGH);
  digitalWrite(ETH_CS,HIGH);
}

Please help me, i want both DHCP and Static modes in my project.

Does your PC have a dhcp server set up on the ethernet interface?

How can i check this, whether my PC has or not.

When connected to network, it uses automatic setting as shown in the attached image.

But i use
IP Address = 192.168.1.10
Subnet Mask = 255.255.255.0
Gateway = 192.168.1.1

This part is optional, i just did this for testing purpose.
Preferred DNS Server = 192.168.1.2
Alternate DNS Server = 192.168.1.3

I set IP manually and also leave it to automatic configuration but nothing works.
How can i check what you are saying??

If you don't know, it doesn't. You must install a dhcp server on the PC's ethernet interface. It does not have a dhcp server installed and set up by default.

Thanks SurferTim

I don't have knowledge of DHCP server, i am using everything directly.

The only problem is that i had not setup a DHCP server on PC, and hence my pc was not able to assign IP address to the Ethernet Shield.

Then i found the DHCP Server software from this link http://www.dhcpserver.de/cms/ After reading the readme.txt file i am able to assign the IP address to my Ethernet Shield.

If someone is facing the same problem, have a look at this video

https://www.youtube.com/watch?v=4xwwV_s9Gik&list=UUNNLL5gqyn0_8OEAKS8zzuQ

xpress_embedo: If someone is facing the same problem, have a look at this video

https://www.youtube.com/watch?v=4xwwV_s9Gik&list=UUNNLL5gqyn0_8OEAKS8zzuQ

Thanks. Very useful...