"use client"; import { useState } from "react"; import { Network, Search, Calculator } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { toast } from "sonner"; export default function IpCalcPage() { const [ip, setIp] = useState("192.168.1.1"); const [mask, setMask] = useState("24"); const [result, setResult] = useState(null); const calculate = () => { try { const parts = ip.split('.').map(Number); if (parts.length !== 4 || parts.some(p => p < 0 || p > 255 || isNaN(p))) { toast.error("无效的 IP 地址"); return; } const maskNum = parseInt(mask); if (isNaN(maskNum) || maskNum < 0 || maskNum > 32) { toast.error("无效的掩码 (0-32)"); return; } // Convert IP to 32-bit number const ipInt = (parts[0] << 24) >>> 0 | (parts[1] << 16) >>> 0 | (parts[2] << 8) >>> 0 | parts[3] >>> 0; // Calculate Mask const maskInt = maskNum === 0 ? 0 : (~0 << (32 - maskNum)) >>> 0; // Network & Broadcast const netInt = (ipInt & maskInt) >>> 0; const broadInt = (netInt | ~maskInt) >>> 0; const intToIp = (i: number) => [ (i >>> 24) & 0xFF, (i >>> 16) & 0xFF, (i >>> 8) & 0xFF, i & 0xFF ].join('.'); setResult({ address: ip, netmask: intToIp(maskInt), wildcard: intToIp(~maskInt >>> 0), network: intToIp(netInt), broadcast: intToIp(broadInt), hostMin: intToIp(netInt + 1), hostMax: intToIp(broadInt - 1), hosts: maskNum >= 31 ? 0 : Math.pow(2, 32 - maskNum) - 2, cidr: `/${maskNum}` }); } catch { toast.error("计算出错"); } }; return (

IP 地址计算

子网掩码、网络地址、可用主机范围计算

网络配置
setIp(e.target.value)} placeholder="如: 192.168.1.1" />
setMask(e.target.value)} placeholder="如: 24" type="number" />
计算结果 {!result ? (

请输入配置并点击计算

) : (
)}
); } function ResultItem({ label, value, cidr }: { label: string; value: string; cidr?: string }) { return (
{label}
{value} {cidr && {cidr}}
); }