简体   繁体   English

最好在依赖网络的代码中使用IP地址或主机名?

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

If you are programming an application which is heavily using the network (eg pings, dns resolves, etc), is it better to use IP addresses in the code (eg if making a folder path on a server - //192.183.181.182/test/test1 for example), or hostnames? 如果您正在编写一个大量使用网络的应用程序(例如ping,dns解析等),那么在代码中使用IP地址是否更好(例如,如果在服务器上创建文件夹路径 - //192.183.181.182/test / test1例如)或主机名?

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. 我假设IP地址,因为它们在企业中往往是静态的,并且主机名可以在单个IP地址上具有别名等。 Is this a valid reason to prefer IP addresses? 这是偏好IP地址的正当理由吗?

Thanks 谢谢

使用硬编码IP地址的问题是,如果您需要更改它,您需要更改代码,而如果您使用主机名,如果您需要更改它,您可以更改主机名指向的位置,而无需触及代码:)

Fully agreeing with jcarlosn above, and throwing in my $0.02 as a network infrastructure guy... 完全同意上面的jcarlosn,并投入我0.02美元作为网络基础设施的人......

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. 作为一名网络工程师,我可以继续讨论我看到人们错误地将IP硬编码到应用程序中的实例数量,以及多年来因为公司不得不扭曲增长而遭受的痛苦。围绕最初假设硬编码的ip和某个特定设施中的子网的IT基础设施。 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. 还要考虑当该应用程序部署在数百台PC上时会发生什么,现在已成为您公司和业务合作伙伴的关键业务。

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. 请使用DNS名称,或者如果您认为必须使用ip出于某些奇怪的原因...使用本地应用程序配置文件,如另一篇文章中所建议的那样。 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. 请理解,配置文件不会更改关键业务应用程序的企业停机时间,以便迁移到新的IP地址。 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. 如果您的桌面用户很好,他们可能会像您迁移DNS名称一样快速重写应用程序配置文件...如果您在桌面部署部门没有真正的脚本技能的公司工作,请重新考虑使用DNS而不是A记录上的TTL超过30分钟。

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). 多宿主服务器在使用名称时会引起问题(如您所述),但IP地址也不一定稳定 - 尤其是在不使用预留的DHCP环境中(比您想象的更常见)。

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. 如果是我,我将路径/主机名/ IP放在配置中 - 然后您可以更改为所需的任何内容而无需重建代码。 If you're doing name lookups yourself internally, just make sure your code is prepared for either an IP or a hostname. 如果您在内部进行名称查找,请确保您的代码已准备好用于IP或主机名。

IPv4 addresses (32bit addresses) are supposed to become obsolete in 2012, (I think they made a movie about that, with earthquakes and tsunamis.) IPv4地址(32位地址)应该在2012年过时了(我认为他们制作了一部关于地震和海啸的电影。)

There are already no new IPv4 addresses available for sale. 目前还没有新的IPv4地址可供出售。

The new standard, IPv6, uses.much longer addresses. 新标准IPv6使用了更长的地址。

Let the user decide, if he/she has a hostname, store that. 让用户决定,如果他/她有主机名,则存储该主机名。 However, hostname may have more than 1 IP address. 但是,主机名可能包含多个IP地址。

If user provides an IP address (v4 or v6), store that. 如果用户提供IP地址(v4或v6),请存储该IP地址。

If you get q MAC address, store that. 如果你得到q MAC地址,请存储。

Explain the users pros and cons (eg names ate more static). 解释用户的利弊(例如,名字吃得更静)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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