运行程序时,控制台窗口立即关闭

时间:2016-10-18 15:00:54

标签: c# ip-address subnet

它只是一个简单的IP掩码计算器,但是当我运行程序时,我希望它显示广播和网络地址,但是当我运行它时窗口会立即关闭。有人能告诉我我做错了吗?

    namespace ConsoleApplication5
    {
       public static class Project3
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
            var ip1 = IPAddress.Parse("192.168.0.1");
            var ip2 = IPAddress.Parse("192.168.1.40");
            var mask = IPAddress.Parse("255.255.255.0");
            bool inSameNet = ip1.IsInSameSubnet(ip2, mask);
            IPAddress broadcast = GetBroadcastAddress(ip2, mask);
            IPAddress net = GetNetworkAddress(ip2, mask);
            Console.WriteLine(broadcast);
            Console.WriteLine(net);
        }
        public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
        {
            byte[] addressBytes = address.GetAddressBytes();
            byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

            if (addressBytes.Length != subnetMaskBytes.Length)
                throw new ArgumentException("No Match.");

            byte[] broadcastAddress = new byte[addressBytes.Length];
            for (int i = 0; i < broadcastAddress.Length; i++)
            {
                broadcastAddress[i] = (byte)(addressBytes[i] | (subnetMaskBytes[i] ^ 255));
            }
            return new IPAddress(broadcastAddress);
        }

        public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
        {
            byte[] netAddressBytes = address.GetAddressBytes();
            byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

            if (netAddressBytes.Length != subnetMaskBytes.Length)
                throw new ArgumentException("No match.");

            byte[] broadcastAddress = new byte[netAddressBytes.Length];
            for (int i = 0; i < broadcastAddress.Length; i++)
            {
                broadcastAddress[i] = (byte)(netAddressBytes[i] & (subnetMaskBytes[i]));
            }
            return new IPAddress(broadcastAddress);
        }

        public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
        {
            IPAddress network1 = address.GetNetworkAddress(subnetMask);
            IPAddress network2 = address2.GetNetworkAddress(subnetMask);

            return network1.Equals(network2);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在main方法结束时添加Console.ReadKey()以使程序等待按键

static void Main(string[] args)
    {
        Console.WriteLine("Hello");
        var ip1 = IPAddress.Parse("192.168.0.1");
        var ip2 = IPAddress.Parse("192.168.1.40");
        var mask = IPAddress.Parse("255.255.255.0");
        bool inSameNet = ip1.IsInSameSubnet(ip2, mask);
        IPAddress broadcast = GetBroadcastAddress(ip2, mask);
        IPAddress net = GetNetworkAddress(ip2, mask);
        Console.WriteLine(broadcast);
        Console.WriteLine(net);
        Console.ReadKey();
    }

答案 1 :(得分:-2)

添加Console.ReadLine();到主要功能的结尾。这将使应用程序等待用户按Enter键。

static void Main(string[] args)
    {
        Console.WriteLine("Hello");
        var ip1 = IPAddress.Parse("192.168.0.1");
        var ip2 = IPAddress.Parse("192.168.1.40");
        var mask = IPAddress.Parse("255.255.255.0");
        bool inSameNet = ip1.IsInSameSubnet(ip2, mask);
        IPAddress broadcast = GetBroadcastAddress(ip2, mask);
        IPAddress net = GetNetworkAddress(ip2, mask);
        Console.WriteLine(broadcast);
        Console.WriteLine(net);
        Console.ReadLine();
    }