开发者

Better to use IP addresses or hostnames in code which relies on the network?

开发者 https://www.devze.com 2023-02-27 08:44 出处:网络
If you are programming an application which is heavily using the network (e.g. pings, dns resolves, etc), is it better to use IP addresses in the code (e.g. if making a folder path on a server - //192

If you are programming an application which is heavily using the network (e.g. pings, dns resolves, etc), is it better to use IP addresses in the code (e.g. if making a folder path on a server - //192.183.181.182/test/test1 for example), or hostnames?

I am assuming IP addresses as they tend to be static in the enterprise, and hostnames can have aliases etc to a single IP address. Is this a valid r开发者_如何学Pythoneason to prefer IP addresses?

Thanks


The problem with using hardcoded IP addresses is that if you need to change it, you need to change the code, while if you use hostnames, if you need to change it, you can change where the hostname points, without touching the code :)


Fully agreeing with jcarlosn above, and throwing in my $0.02 as a network infrastructure guy...

As a network engineer, I could go on for quite some time about the number of instances where I've seen people mistakenly hard-code an IP into an application, and the suffering that happens for years because the company has to contort the growth of IT infrastructure around that original assumption of a hardcoded ip and a subnet in some particular facility. This has an impact on operational and capital expenses of the company.

Also consider what happens when that app is deployed on hundreds of PCs, and has now become business critical to your company and business partners.

Please use DNS names, or if you feel you must use an ip for some bizarre reason... use a local application config file as suggested in another post. Please understand though that a config file doesn't change the corporate downtime for a business-critical app to get migrated to a new IP address. If your desktop people are good, they can probably rewrite an application config file as quickly as you could migrate a DNS name... if you work for a company with no real scripting skills in the desktop deployment dept, please reconsider using DNS with no longer than a 30 minute TTL on that A record.


There are valid reasons for both. Multi-homed servers can cause issues when using names (as you mention), but IP addresses aren't necessarily stable either- especially in a DHCP environment that's not using reservations (more common than you might think).

If it were me, I'd put the path/hostname/IP in config- then you can change to whatever's needed without rebuilding the code. If you're doing name lookups yourself internally, just make sure your code is prepared for either an IP or a hostname.


IPv4 addresses (32bit addresses) are supposed to become obsolete in 2012, (I think they made a movie about that, with earthquakes and tsunamis.)

There are already no new IPv4 addresses available for sale.

The new standard, IPv6, uses.much longer addresses.

Let the user decide, if he/she has a hostname, store that. However, hostname may have more than 1 IP address.

If user provides an IP address (v4 or v6), store that.

If you get q MAC address, store that.

Explain the users pros and cons (e.g. names ate more static).

0

精彩评论

暂无评论...
验证码 换一张
取 消