Пример #1
0
        private void ConnectionCheckRunner()
        {
            while (true)
            {
                try
                {
                    var addr = InetAddress.GetByName("192.168.1.248");

                    if (addr.IsReachable(50))
                    {
                        RunOnUiThread(() => _dronStateTextView.SetBackgroundColor(new Color(0, 255, 0)));
                    }
                    else
                    {
                        RunOnUiThread(() => _dronStateTextView.SetBackgroundColor(new Color(255, 0, 0)));
                    }

                    Thread.Sleep(100);
                }
                catch (UnknownHostException)
                {
                    RunOnUiThread(() => _dronStateTextView.SetBackgroundColor(new Color(255, 0, 0)));
                }
                catch (IOException)
                {
                    RunOnUiThread(() => _dronStateTextView.SetBackgroundColor(new Color(255, 0, 0)));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Tests if a host name is pingable
        /// </summary>
        /// <param name="host">The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address (127.0.0.1)</param>
        /// <param name="msTimeout">Timeout in milliseconds</param>
        /// <returns></returns>
        public override async Task <bool> IsReachable(string host, int msTimeout = 5000)
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("host");
            }

            if (!IsConnected)
            {
                return(false);
            }

            return(await Task.Run(() =>
            {
                bool reachable;
                try
                {
                    reachable = InetAddress.GetByName(host).IsReachable(msTimeout);
                }
                catch (UnknownHostException ex)
                {
                    Debug.WriteLine("Unable to reach: " + host + " Error: " + ex);
                    reachable = false;
                }
                catch (Exception ex2)
                {
                    Debug.WriteLine("Unable to reach: " + host + " Error: " + ex2);
                    reachable = false;
                }
                return reachable;
            }));
        }
        private void SetButtonClick(object sender, EventArgs e)
        {
            IpConfiguration ipConfiguration = GetConf();

            if (ipConfiguration == null)
            {
                return;
            }

            if (radio_dhcp.Checked)
            {
                SetConf(new IpConfiguration(IpConfiguration.IpAssignment.Dhcp, null, IpConfiguration.ProxySettings.Unassigned, null));
            }
            else if (radio_static.Checked)
            {
                List <InetAddress> dnsList = new List <InetAddress>();
                dnsList.Add(InetAddress.GetByName(dns.Text));
                IpConfiguration ipConf = new IpConfiguration(
                    IpConfiguration.IpAssignment.Static,
                    new StaticIpConfiguration(ipaddress.Text, InetAddress.GetByName(gateway.Text), dnsList, null),
                    IpConfiguration.ProxySettings.Unassigned,
                    null);

                SetConf(ipConf);
            }
        }
        public async Task <bool> IsPingReachable(string host, int msTimeout = 5000)
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("host");
            }

            if (!IsConnected)
            {
                return(false);
            }

            return(await Task.Run(() =>
            {
                bool reachable;
                try
                {
                    reachable = InetAddress.GetByName(host).IsReachable(msTimeout);
                }
                catch (UnknownHostException)
                {
                    reachable = false;
                }
                return reachable;
            }));
        }
Пример #5
0
        /// <summary>
        /// 获取链接到当前热点的设备IP:
        /// </summary>
        public static List <WifiApStateViewModel> GetConnectedHotIp()
        {
            var    result = new List <WifiApStateViewModel>();
            var    fr     = new FileReader("/proc/net/arp");
            var    br     = new BufferedReader(fr);
            string line;

            while ((line = br.ReadLine()) != null)
            {
                var splitted = Regex.Split(line, " +");
                if (splitted.Length < 4 || splitted[0] == "IP")
                {
                    continue;
                }
                var es    = Executors.NewCachedThreadPool();
                var model = new WifiApStateViewModel()
                {
                    Ip    = splitted[0],
                    State = splitted[2]
                };
                es.Submit(new Runnable(() =>
                {
                    var ip = InetAddress.GetByName(splitted[0]);
                    if (ip.IsReachable(1000))
                    {
                        model.State = "1";
                    }
                }));
                result.Add(model);
            }
            fr.Close();
            br.Close();
            return(result);
        }
Пример #6
0
 private void AddToOutboundQueue(byte[] data, Bundle rinfo)
 {
     try {
         outboundQueue.Enqueue(new DatagramPacket(data, data.Length,
                                                  InetAddress.GetByName(rinfo.GetString(midi.MIDIConstants.RINFO_ADDR)), rinfo.GetInt(midi.MIDIConstants.RINFO_PORT)));
         selector.Wakeup();
     } catch (UnknownHostException e) {
         throw new UnknownHostException(e.StackTrace);
     }
 }
 private bool isInternetReachable()
 {
     try
     {
         var address = InetAddress.GetByName("google.com");
     }
     catch (Exception e)
     {
         return(false);
     }
     return(true);
 }
Пример #8
0
 public override Task <bool> IsHostReachable(string host)
 {
     return(Task <bool> .Run(() => {
         try {
             return InetAddress
             .GetByName(host)
             .IsReachable(5000);
         }
         catch {
             return false;
         }
     }));
 }
Пример #9
0
 public Task <bool> IsReachable(string host, TimeSpan timeout)
 {
     return(Task <bool> .Run(() =>
     {
         try
         {
             var address = InetAddress.GetByName(host);
             return address == null ? false : address.IsReachable((int)timeout.TotalMilliseconds);
         }
         catch (Java.Net.UnknownHostException)
         {
             return false;
         }
     }));
 }
        public override Task <bool> GetHostReachableAsync(string host, CancellationToken token = default(CancellationToken))
        {
            return(Task.Run(() =>
            {
                if (string.IsNullOrEmpty(host))
                {
                    throw new ArgumentNullException(nameof(host));
                }

                try {
                    return InetAddress.GetByName(host).IsReachable(5000);
                }
                catch (UnknownHostException) {
                    return false;
                }
            }, token));
        }
Пример #11
0
        public void ScanDeviceThread()
        {
            int BEGANNum = 0; //开始扫描的起始
            int ENDNum   = 0; //开始扫描的结束
            int mTheardNum;

            lock (this)
            {
                mTheardNum = TheardNum++;//当前是第几个线程
            }
            BEGANNum = mTheardNum * 256 / ScanDeviceTheardNum;
            ENDNum   = BEGANNum + 256 / ScanDeviceTheardNum;
            ConnectivityManager cm            = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         activeNetwork = cm.ActiveNetworkInfo;
            WifiManager         wm            = (WifiManager)context.GetSystemService(Context.WifiService);

            WifiInfo connectionInfo = wm.ConnectionInfo;
            int      ipAddress      = connectionInfo.IpAddress;
            string   ipString       = Formatter.FormatIpAddress(ipAddress);

            string prefix = ipString.Substring(0, ipString.LastIndexOf(".") + 1);

            for (int i = BEGANNum; i < ENDNum; i++)//执行扫描
            {
                string      testIp    = prefix + i.ToString();
                InetAddress address   = InetAddress.GetByName(testIp);
                bool        reachable = address.IsReachable(800);
                string      hostName  = address.CanonicalHostName;
                if (reachable)//如果Ping通 吧IP加入
                {
                    lock (this)
                    {
                        DeviceIP.Add(testIp, "");
                    }
                }
            }
            TheardComplete++;                          //线程完成数加1
            if (TheardComplete >= ScanDeviceTheardNum) //如果这个是最后一个完成的线程
            {
                Log.Info("TCP", "StartConnectDevice : " + DeviceIP.Count.ToString());
                TheardComplete = 0;
                TheardNum      = 0;
                ConnectDevice();//开始尝试连接
            }
        }
Пример #12
0
        /// <summary>
        /// Determines whether the specified host is reachable.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="timeout">The timeout.</param>
        public Task <bool> IsReachable()
        {
            return(Task.Run(
                       () =>
            {
                try
                {
                    var address = InetAddress.GetByName(CommonSettings.ServiceHost);


                    return address != null;     // && (address.IsReachable((int)timeout.TotalMilliseconds) || );
                }
                catch (UnknownHostException)
                {
                    return false;
                }
            }));
        }
Пример #13
0
        private void RetrieveIp()
        {
            Task.Run(() =>
            {
                try
                {
                    var configuration = Mvx.Resolve <IConfiguration>();

                    var baseUrl = configuration.BaseUrl.Replace("https://", "").Replace("/", "");

                    var address = InetAddress.GetByName(baseUrl);

                    ViewModel.ServerIp = address.CanonicalHostName;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            });
        }
Пример #14
0
        public override async Task <bool> executeAsync(IInfoContext info, FunctionContext context)
        {
            string host      = "google.com";
            int    msTimeout = 5000;

            return(await Task.Run <bool> (() => {
                bool reachable;
                try {
                    log(LogLevel.Info, "Connect to internet....");
                    reachable = InetAddress.GetByName(host).IsReachable(msTimeout);
                } catch (UnknownHostException ex) {
                    log("Unable to reach: " + host + " Error: " + ex);
                    reachable = false;
                } catch (Exception ex2) {
                    log("Unable to reach: " + host + " Error: " + ex2);
                    reachable = false;
                }
                return reachable;
            }));
        }
Пример #15
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            try
            {
                InetAddress    multicastAddress = InetAddress.GetByName("192.168.2.229");
                DatagramSocket socket           = new DatagramSocket(5004);
                byte[]         buff             = new byte[18];

                DatagramPacket packet = new DatagramPacket(buff, buff.Length);
                socket.ConnectAsync(multicastAddress, 5008);
                socket.ReceiveAsync(packet);
            }
            catch (Exception e)
            {
            }
        }
Пример #16
0
        /** 
         * Starts a stream in a synchronous manner. <br />
         * Throws exceptions in addition to calling a callback.
         * @param id The id of the stream to start
         **/
        public void syncStart(int id)
        {

            Stream stream = id == 0 ? (Stream)mAudioStream : mVideoStream;
            if (stream != null && !stream.isStreaming()) {
                try {
                    InetAddress destination = InetAddress.GetByName(mDestination);
                    stream.setTimeToLive(mTimeToLive);
                    stream.setDestinationAddress(destination);
                    stream.start();
                    if (getTrack(1 - id) == null || getTrack(1 - id).isStreaming()) {
                        postSessionStarted();
                    }
                    if (getTrack(1 - id) == null || !getTrack(1 - id).isStreaming()) {
                        mHandler.Post(mUpdateBitrate);
                    }
                } catch (UnknownHostException e) {
                    postError(ERROR_UNKNOWN_HOST, id, e);
                    throw e;
                } catch (CameraInUseException e) {
                    postError(ERROR_CAMERA_ALREADY_IN_USE, id, e);
                    throw e;
                } catch (StorageUnavailableException e) {
                    postError(ERROR_STORAGE_NOT_READY, id, e);
                    throw e;
                } catch (ConfNotSupportedException e) {
                    postError(ERROR_CONFIGURATION_NOT_SUPPORTED, id, e);
                    throw e;
                } catch (InvalidSurfaceException e) {
                    postError(ERROR_INVALID_SURFACE, id, e);
                    throw e;
                } catch (IOException e) {
                    postError(ERROR_OTHER, id, e);
                    throw e;
                } catch (RuntimeException e) {
                    postError(ERROR_OTHER, id, e);
                    throw e;
                }
            }

        }
Пример #17
0
        public bool IsHostPingReachable(string host)
        {
            bool reachable = false;

            if (IsConnected)
            {
                if (!string.IsNullOrEmpty(host))
                {
                    try
                    {
                        reachable = InetAddress.GetByName(host).IsReachable(ReachableTimeoutInMilliseconds);
                    }
                    catch (UnknownHostException)
                    {
                        reachable = false;
                    }
                }
            }

            return(reachable);
        }
        /// <summary>
        /// Gets a list of the clients connected to the Hotspot
        /// </summary>
        /// <param name="onlyReachables"><code>false</code> if the list should contain unreachable (probably disconnected) clients, <code>true </code> otherwise</param>
        /// <param name="reachableTimeout">Reachable timeout in milliseconds</param>
        /// <returns>List of <see cref="ClientScanResult"/></returns>
        public Task <IList <ClientScanResult> > GetClientListAsync(bool onlyReachables, int reachableTimeout)
        {
            return(Task.Run(() =>
            {
                IList <ClientScanResult> result = new List <ClientScanResult>();

                var macRegex = new Regex("..:..:..:..:..:..", RegexOptions.Compiled);

                try
                {
                    var lines = File.ReadAllLines("/proc/net/arp");
                    foreach (var line in lines)
                    {
                        string[] splitted = line.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

                        if (splitted != null && splitted.Length >= 4)
                        {
                            //Basic sanity check
                            string mac = splitted[3];

                            if (macRegex.IsMatch(mac))
                            {
                                bool isReachable = InetAddress.GetByName(splitted[0]).IsReachable(reachableTimeout);

                                if (!onlyReachables || isReachable)
                                {
                                    result.Add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                                }
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    Log.Error(nameof(WifiApManager), "", ex);
                }

                return result;
            }));
        }
Пример #19
0
        /// <inheritdoc />
        public async Task <bool> IsHostReachable(string host, int msTimeout = 5000)
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException(nameof(host));
            }

            if (!IsConnected)
            {
                return(false);
            }

            host = host.Replace("http://www.", string.Empty).
                   Replace("http://", string.Empty).
                   Replace("https://www.", string.Empty).
                   Replace("https://", string.Empty).
                   TrimEnd('/');

            return(await Task.Run(() =>
            {
                bool reachable;
                try
                {
                    reachable = InetAddress.GetByName(host).IsReachable(msTimeout);
                }
                catch (UnknownHostException ex)
                {
                    Logger.Warn("Unable to reach: " + host + " Error: " + ex);
                    reachable = false;
                }
                catch (Exception ex2)
                {
                    Logger.Warn("Unable to reach: " + host + " Error: " + ex2);
                    reachable = false;
                }
                return reachable;
            }));
        }
Пример #20
0
        public async Task <bool> IsPingReachable()
        {
            string host      = @"http://fit-bachelor.azurewebsites.net/";
            int    msTimeout = 10000;

            if (!IsConnected)
            {
                return(false);
            }

            return(await Task.Run(() =>
            {
                bool reachable;
                try
                {
                    reachable = InetAddress.GetByName(host).IsReachable(msTimeout);
                }
                catch (UnknownHostException)
                {
                    reachable = false;
                }
                return reachable;
            }));
        }