简体   繁体   中英

What is the benefit of Arduino Ethernet's library IPAddress()?

The Arduino default Ethernet library class contains an IPAddress variable type. What is this IPAddress for? Why should I use it and why is it not used for the gateway and subnet IPs in the official example ?

It just, like you said, a type of variable (such as int (integer)) that can store a IP address. Using integers, you cannot add the . s needed in the IP address. Also, the library only accepts integers, because with strings, things "can get messy." For example, if you have 1 in a string, you cannot add that with another number. However, if you have the integer variable type with the value of 1 , it would add easily.


How can I use this?:

On Arduino's EthernetIpAdress page , there is this code:

 #include <Ethernet.h>

 // network configuration.  gateway and subnet are optional.

  // the media access control (ethernet hardware) address for the shield:
 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
 // the router's gateway address:
 byte gateway[] = { 10, 0, 0, 1 };
 // the subnet:
 byte subnet[] = { 255, 255, 0, 0 };

 EthernetServer server = EthernetServer(23);

 //the IP address is dependent on your network
 IPAddress ip(192,168,1,1);
 void setup()
 {
   // initialize the ethernet device
   Ethernet.begin(mac, ip, gateway, subnet);

   // start listening for clients
   server.begin();
 }
 void loop()
 {
   //print out the IP address
   Serial.println(myIPaddress);
 }

On the line IPAddress ip(192,168,1,1); , it creates a variable that holds the IP address. In the line Ethernet.begin(mac, ip, gateway, subnet); the variable is looked up and given to the Ethernet library. I don't know what the advantage would be, besides trying to prevent people from using the integer type and making it look cleaner. It could look up the automatically issued IP address and then store it for later so if it goes into an "idle mode," it can ask for the same IP address so it is almost like a dynamic IP that wouldn't interfere with other devices and would reset when the reset button is pushed. I'm sure that there is some use for it, but I cannot think of one. I just wanted to tell you what it is and how to use it. I would think though that it would be easier just to use #define IPadress 192.168.1.1 or a similar thing if you wanted it to easily be changed or to be more user readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM