用js写一个网络和IP地址计算器
时间: 2024-02-21 17:59:34 浏览: 112
网络地址计算器
5星 · 资源好评率100%
下面是一个简单的使用 JavaScript 实现的网络和 IP 地址计算器,可以计算出给定 IP 地址和子网掩码对应的网络地址、广播地址、可用主机地址范围等信息。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IP地址计算器</title>
</head>
<body>
<h1>IP地址计算器</h1>
<form>
<label for="ipaddress">IP地址:</label>
<input type="text" id="ipaddress" name="ipaddress"><br>
<label for="subnetmask">子网掩码:</label>
<input type="text" id="subnetmask" name="subnetmask"><br>
<button type="button" onclick="calculate()">计算</button>
</form>
<h2>计算结果</h2>
<p id="networkaddress"></p>
<p id="broadcastaddress"></p>
<p id="hostrange"></p>
<script>
function calculate() {
var ip = document.getElementById("ipaddress").value;
var subnet = document.getElementById("subnetmask").value;
var ipArray = ip.split(".");
var subnetArray = subnet.split(".");
// Convert IP address and subnet mask to binary
var ipBinary = "";
for (var i = 0; i < ipArray.length; i++) {
ipBinary += ("00000000" + parseInt(ipArray[i], 10).toString(2)).slice(-8);
}
var subnetBinary = "";
for (var j = 0; j < subnetArray.length; j++) {
subnetBinary += ("00000000" + parseInt(subnetArray[j], 10).toString(2)).slice(-8);
}
// Calculate network address
var networkBinary = "";
for (var k = 0; k < 32; k++) {
if (subnetBinary.charAt(k) == "1") {
networkBinary += ipBinary.charAt(k);
} else {
networkBinary += "0";
}
}
var networkAddress = "";
for (var l = 0; l < 4; l++) {
networkAddress += parseInt(networkBinary.substr(l * 8, 8), 2) + ".";
}
networkAddress = networkAddress.slice(0, -1);
// Calculate broadcast address
var broadcastBinary = "";
for (var m = 0; m < 32; m++) {
if (subnetBinary.charAt(m) == "1") {
broadcastBinary += networkBinary.charAt(m);
} else {
broadcastBinary += "1";
}
}
var broadcastAddress = "";
for (var n = 0; n < 4; n++) {
broadcastAddress += parseInt(broadcastBinary.substr(n * 8, 8), 2) + ".";
}
broadcastAddress = broadcastAddress.slice(0, -1);
// Calculate host range
var hostRangeBinary = "";
for (var p = 0; p < 32; p++) {
if (subnetBinary.charAt(p) == "1") {
hostRangeBinary += networkBinary.charAt(p);
} else {
hostRangeBinary += "1";
}
}
var numberOfHosts = Math.pow(2, 32 - subnetBinary.replace(/0/g, "").length) - 2;
var firstHostBinary = hostRangeBinary.slice(0, -1) + "0";
var firstHostAddress = "";
for (var q = 0; q < 4; q++) {
firstHostAddress += parseInt(firstHostBinary.substr(q * 8, 8), 2) + ".";
}
firstHostAddress = firstHostAddress.slice(0, -1);
var lastHostBinary = hostRangeBinary.slice(0, -1) + "1";
var lastHostAddress = "";
for (var r = 0; r < 4; r++) {
lastHostAddress += parseInt(lastHostBinary.substr(r * 8, 8), 2) + ".";
}
lastHostAddress = lastHostAddress.slice(0, -1);
var hostRange = firstHostAddress + " - " + lastHostAddress + " (" + numberOfHosts + " hosts)";
// Display results
document.getElementById("networkaddress").innerHTML = "网络地址:" + networkAddress;
document.getElementById("broadcastaddress").innerHTML = "广播地址:" + broadcastAddress;
document.getElementById("hostrange").innerHTML = "主机地址范围:" + hostRange;
}
</script>
</body>
</html>
```
该计算器使用了 `split()` 函数将 IP 地址和子网掩码按照点号分割成数组,然后使用 `parseInt()` 函数将每个数组元素转换成十进制数,再使用 `toString(2)` 函数将其转换成二进制数,最后使用 `slice()` 函数补足前导零,得到 IP 地址和子网掩码的二进制形式。然后,使用循环和逻辑运算计算出网络地址、广播地址和主机地址范围,最后将结果输出到 HTML 页面中。
阅读全文