how to find out DHCP-server's IP-address? [SOLVED ...BUT]

When getting an IP-address from an dhcp-server, you'll get a localIP, subnetMask, gatewayIP, dnsServerIP.

But is there a way to know the dhcp-server's IP address? something like dhcpServerIP ?

The DHCP class has a public method for fetching the DHCP server IP address:

IPAddress DhcpClass::getDhcpServerIp();

Unfortunately for you the Ethernet class keeps it's pointer to the DHCP object (_dhcp) private. If it didn't you could call:

IPAddress dhcpAddress = Ethernet._dhcp->getDhcpServerIp();

You can edit Ethernet.h to make that pointer public but your code would not be portable.

You could do your own DHCP request and pass the results to Ethernet.begin(). That would be the supported way. Just copy the code from EthernetClass::begin(uint8_t *mac_address) and when you have the DHCP results, call Ethernet.begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet).

Thanks,

I do understand what you're saying, but let's say I'm happy using Ethernet.h and I only want to do a DHCP request to fetch the server's IP, not more

How would one do that?
obviously I would only need a small part?

(I'm going to try to find out myself too)

Something like:

#include "Dhcp.h"
  IPAddress DhcpServerIp;
  DhcpClass *dhcp = new DhcpClass();

  // Now try to get our config info from a DHCP server
  int ret = dhcp->beginWithDHCP(mac_address);
  if(ret == 1)
  {
    DhcpIPServerAddress = dhcp-> getDhcpServerIp()
  }

Because this is a separate DHCP object from the one Ethernet is using the DHCP server you find may be different from the one that Ethernet found.

Because this is a separate DHCP object from the one Ethernet is using the DHCP server you find may be different from the one that Ethernet found.

Yes, I came to that conclusion myself too. And it is not the kind of behaviour I want, if I get an IP, I most now for sure what dhcp client it came from. So your first post, is the way to solve the problem.

I'll just have to include the changed ethernet.h within the sketch and that should make it portable, doesn't it?

OK, I figured out what files I had to change:

Ethernet.h
Ethernet.cpp

But this means, every one wanting to use this sketch, will have to change there Ethernet Library (I attached my files). Can't I just include these files into my sketch? I tried todo so, but the Arduino IDE complains about a conflict between the original files (in the library folder) and the ones in my sketch. Why?

OR maybe even better, Can't I submit my change? So that everyone can use this: finding out your dhcp-servers IP-address.

Ethernet.h (1.2 KB)

Ethernet.cpp (2.69 KB)

OR maybe even better, Can't I submit my change? So that everyone can use this: finding out your dhcp-servers IP-address.

Yes, you can. Do a search for SurferTim's name. He's posted details about the change requests he's submitted, including how and where.