Esempio n. 1
0
        /// <summary>
        /// 发送配置信息
        /// </summary>
        /// <param name="cmd">需要配置的规则</param>
        public bool  SendConfigInfo(string cmd)
        {
            config_info_confirm = false;

            byte[]     head        = { 0x0f, 0x0e, 0x0d }; //自定义数据包包头
            byte[]     body        = Encoding.ASCII.GetBytes(cmd + "!");
            byte[]     data        = head.Concat(body).ToArray();
            UdpClient  client      = null;
            IPAddress  remoteIP    = IPAddress.Parse(devform.getDev_IP());
            int        remotePort  = devform.getDev_port();
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);

            CaptureDeviceList devices = CaptureDeviceList.Instance;

            // If no devices were found print an error
            if (devices.Count < 1)
            {
                // Console.WriteLine("No devices were found on this machine");
                return(false);
            }
            ICaptureDevice device = devices[0];

            device.OnPacketArrival += new PacketArrivalEventHandler(configDev_OnPacketArrival);
            int readTimeoutMilliseconds = 1000;

            device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
            string filter = "ip and udp";

            device.Filter = filter;
            device.StartCapture();

#if debug
            Console.WriteLine("start sending:");
#endif
            client = new UdpClient();
            client.Send(data, data.Length, remotePoint);

            /* 监听无IP配置是否成功返回消息  */
            bool       done     = false;
            UdpClient  listener = new UdpClient(30333);
            IPEndPoint groupEP  = null;
            //DateTime beforDT = System.DateTime.Now;
            int n = 0;
            try
            {
                while (!done && n <= 10)
                {
                    byte[] content = listener.Receive(ref groupEP);
                    string con     = Encoding.Default.GetString(content);
                    if (con == "yes")
                    {
                        done = true;
                        config_info_confirm = true;
                    }
                    n++;
                    //DateTime afterDT = System.DateTime.Now;
                    //TimeSpan ts = afterDT.Subtract(beforDT);
                    //Console.WriteLine("{0}",ts.TotalSeconds);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                config_info_confirm = false;
            }
            finally
            {
                listener.Close();
            }

            client.Close();
#if debug
            Console.WriteLine("send successfully!");
#endif
            device.StopCapture();
            device.Close();

            return(config_info_confirm);
        }
Esempio n. 2
0
        /// <summary>
        /// 发送配置信息
        /// </summary>
        /// <param name="cmd">需要配置的规则</param>
        public bool  SendConfigInfo(string cmd)
        {
            config_info_confirm = false;

            byte[]        head     = { 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a }; //自定义数据包包头
            byte[]        body     = Encoding.ASCII.GetBytes(cmd + "*");
            byte[]        data     = head.Concat(body).ToArray();
            ASCIIEncoding encoding = new ASCIIEncoding();
            string        yucon    = encoding.GetString(data, 0, data.Length);

            Console.WriteLine("{0}", yucon);

            UdpClient  client      = null;
            IPAddress  remoteIP    = IPAddress.Parse(devform.getDev_IP());
            int        remotePort  = devform.getDev_port();
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);

            //CaptureDeviceList devices = CaptureDeviceList.Instance;
            //// If no devices were found print an error
            //if (devices.Count < 1)
            //{
            //    // Console.WriteLine("No devices were found on this machine");
            //    return false;
            //}
            //ICaptureDevice device = devices[0];
            //device.OnPacketArrival += new PacketArrivalEventHandler(configDev_OnPacketArrival);
            //int readTimeoutMilliseconds = 1000;
            //device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
            //string filter = "ip and udp";
            //device.Filter = filter;
            //device.StartCapture();

#if debug
            Console.WriteLine("start sending:");
#endif
            client = new UdpClient();
            client.Send(data, data.Length, remotePoint);


            UdpClient listener = new UdpClient(30333);
            listener.Client.ReceiveTimeout = 10000;
            IPEndPoint groupEP = null;
            try
            {
                while (!config_info_confirm)
                {
                    byte[] content = listener.Receive(ref groupEP);
                    string con     = Encoding.Default.GetString(content);
                    if (con == "success")
                    {
                        config_info_confirm = true;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                config_info_confirm = false;
            }
            finally
            {
                listener.Close();
            }

            client.Close();
#if debug
            Console.WriteLine("send successfully!");
#endif

            return(config_info_confirm);
        }