예제 #1
0
        private PingReply sendPing(string Address, int Timeout)
        {
            PingReply pingReply = null;

            try
            {
                System.Net.NetworkInformation.Ping pingIP = new System.Net.NetworkInformation.Ping();
                Thread a = new Thread(() =>
                {
                    try
                    {
                        pingReply = pingIP.Send(Address);
                    }
                    catch (PingException d) { ITMessageBox.Show($"{d.Message}", $"Ping exception"); return; }
                    catch (Exception g) { ITMessageBox.Show(g.Message); return; }
                });
                a.Start();
                a.Join(Timeout);
                return(pingReply);
            }
            catch { return(null); }
        }
예제 #2
0
        public void SendUDPPing(string Address, int Timeout, int Port)
        {
            PingDataStructure pds = new PingDataStructure();

            var oof = GetHosts(Address);

            pds.FoundIPV4 = oof.FoundIPV4;
            pds.FoundIPV6 = oof.FoundIPV6;

            pds.SuccessOrUnsuccess = "Doesn't work yet :(";

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            Stopwatch sw = new Stopwatch();

            sw.Start();
            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            try { s.BeginConnect(Address, Port, null, null).AsyncWaitHandle.WaitOne(Timeout); } catch (Exception gg) { ITMessageBox.Show(gg.Message, "Err sending UDP Ping"); }
            sw.Stop();

            //pds.SuccessOrUnsuccess = hasConnected ? "Success" : "Failed";
            pds.SourceAddress = GetIPAddresses.GetLocalAddress;
            pds.Protocol      = $"UDP -> {Port}";
            pds.Destination   = Address;
            pds.Time          = sw.Elapsed.TotalMilliseconds.ToString() + " ms";
            pds.Data          = $"{s.SendBufferSize.ToString()} Bytes";

            OnStatusMessage?.Invoke(pds);
        }