当前位置:编程学习 > C#/ASP.NET >>

在 c#下能禁止网络连接么 ?

如题~在c#里能用代码控制 断开网络连接么?或类似防火墙一样的功能监测连接状况 --------------------编程问答-------------------- 可以,利用wmi编程,相关知识可以百度一下! --------------------编程问答-------------------- 掉用API接口 --------------------编程问答-------------------- 可以。禁用网卡,需要调用WINDOWS API --------------------编程问答-------------------- 禁用网卡,需要调用WINDOWS API --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 调用Windows API接口即可。详细步骤参考Windows API Reference --------------------编程问答-------------------- 如果禁用网卡以后还能在系统设置里面打开么? --------------------编程问答--------------------
引用 6 楼 zggggg1 的回复:
http://zhidao.baidu.com/question/157455801.html

fuck! 广告 --------------------编程问答--------------------  调用系统API --------------------编程问答--------------------
引用 6 楼 zggggg1 的回复:
http://zhidao.baidu.com/question/157455801.html


严重鄙视!!!!!!!!!!! --------------------编程问答--------------------
引用 6 楼 zggggg1 的回复:
http://zhidao.baidu.com/question/157455801.html



我也看不起你. --------------------编程问答-------------------- 可以 !  我今天没回帖!(10积分啊) --------------------编程问答-------------------- http://topic.csdn.net/t/20030620/08/1936850.html

参考! --------------------编程问答-------------------- cmd,调用bat文件,里面禁止连接
cmd,调用ping命令,检测网络收发包,用来检测连接状态。

 public static int PingHost(string[] hostclient, int timeOutVal)
        {
            const int ICMP_ECHO = 8;
            const int PING_ERR = -1;
            const int TIME_OUT = 200;

            int retval = 0;

            Socket sock = new Socket(AddressFamily.InterNetwork, 
SocketType.Raw, ProtocolType.Icmp);

            sock.SetSocketOption(SocketOptionLevel.Socket, 
SocketOptionName.SendTimeout, TIME_OUT);
            sock.SetSocketOption(SocketOptionLevel.Socket, 
SocketOptionName.ReceiveTimeout, TIME_OUT);

            IPHostEntry hostInfo = null;
            try
            {
                IPAddress ipaddr = IPAddress.Parse(hostclient[0]);
                hostInfo = Dns.GetHostByName(hostclient[0]);
            }
            catch(Exception)
            {
                retval = PING_ERR;
                goto exit;
            }

            EndPoint hostPoint = (EndPoint) new 
IPEndPoint(hostInfo.AddressList[0], 0);

            IPHostEntry clientInfo = null;
            try
            {
                if (hostclient.Length > 1)
                    clientInfo = Dns.GetHostByAddress(hostclient[1]);
                else
                    clientInfo = Dns.GetHostByName(Dns.GetHostName());
            }
            catch (Exception)
            {
                retval = PING_ERR;
                goto exit;
            }

            EndPoint clientPoint = (EndPoint) new 
IPEndPoint(clientInfo.AddressList[0], 0);

            int dataSize = 32;
            int packetSize = dataSize + 8;

            IcmpPacket packet = new IcmpPacket(ICMP_ECHO, 0, 0, 45, 
UInt16_Seq++, dataSize);

            Byte[] buffer = new Byte[packetSize];
            int index = packet.ConverToByte(buffer);
            if (index != packetSize)
            {
                retval = PING_ERR;
                goto exit;
            }

            int count = (int)Math.Ceiling( (Double)index/2);
            UInt16[] buffer2 = new UInt16[count];

            index = 0;
            for (int i=0; i<count; i++)
            {
                buffer2[i] = BitConverter.ToUInt16(buffer, index);
                index += 2;
            }

            packet.CheckSum = IcmpPacket.SumofCheck(buffer2);

            Byte[] sendData = new Byte[packetSize];
            index = packet.ConverToByte(sendData);

            if (index != packetSize)
            {
                retval = PING_ERR;
                goto exit;
            }

            for (int i=0; i<5; i++)
            {
                int nBytes = 0;
                int startTime = Environment.TickCount;

                try
                {
                    if ((nBytes = sock.SendTo(sendData, packetSize, 
SocketFlags.None, (EndPoint)hostPoint)) == -1)
                    {
                        retval = PING_ERR;
                        goto exit;
                    }
//                  Console.WriteLine(nBytes.ToString());
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Send time out");
                    return -1;
                }

                Byte[] receiveData = new Byte[256];
                nBytes = 0;
                int timeout = 0;
                int timeConsume = 0;
                while (true)
                {
                    try
                    {

                        nBytes = sock.ReceiveFrom(receiveData, 256, 
SocketFlags.None, ref (EndPoint)clientPoint);

                        if (nBytes > 0)
                        {
                            if (packet._sequenceNumber == 
packet.GetSequenceNum(receiveData))
                            {
                                retval = 0;
                                goto exit;
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        return -1;
                    }
                    if (nBytes == -1)
                    {
                        retval = PING_ERR;
                        goto exit;
                    }
                    else if (nBytes > 0)
                    {
                        timeConsume = System.Environment.TickCount - 
startTime;
                        retval = timeConsume;

                    }

                    timeout = System.Environment.TickCount - startTime;
                    if (timeout > timeOutVal)
                    {
                        retval = PING_ERR;
                        goto exit;
                    }
                
                }
            }

exit:       sock.Close();
            return retval;
            
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,