Exemple #1
0
        public static void Execute(string ipaddress)
        {
            string      imageArray  = GetImage();
            Ping        pingSender  = new Ping();
            PingOptions pingOptions = new PingOptions();

            pingOptions.DontFragment = true;

            byte[] buffer     = Encoding.ASCII.GetBytes(imageArray);
            int    bufferSize = buffer.Count <byte>();
            int    index      = 0;
            bool   done       = false;

            while (!done)
            {
                int offset = 1024;

                if ((index + offset) > bufferSize)
                {
                    offset = bufferSize - index;
                }
                byte[] tmpBuffer = new byte[offset];

                Buffer.BlockCopy(buffer, index, tmpBuffer, 0, offset);
                index = index + offset;

                PingReply reply = pingSender.Send(IPAddress.Parse(ipaddress), 120, tmpBuffer, pingOptions);
                reply.ToString();
            }
        }
Exemple #2
0
        public void PingResource()
        {
            // Act
            PingReply result = PingOperation.PingResource("www.google.com");

            // Log
            Console.WriteLine(result.ToString());
            Console.WriteLine(result.Address.ToString());

            // Assert
            Assert.NotNull(result);
        }
        public String CalculatePingTime()
        {
            Ping      ping  = new Ping();
            PingReply reply = ping.Send(InputOne);

            if (!reply.ToString().Equals("Success"))
            {
                if (!reply.RoundtripTime.Equals(0))
                {
                    return(Convert.ToString(reply.RoundtripTime));
                }
                else
                {
                    return("Ping Failed");
                }
            }
            else
            {
                return(reply.ToString());
            }
        }
        private PingReply ExceptionHandler(Ping slp)
        {
            DateTime?FirstTime  = null;
            int      RetryTime  = 1000 * 6;
            int      TryTick    = 0;
            int      MaxTryTick = ushort.MaxValue;

            while (State != States.Abort)
            {
                PingReply SLPResult = null;
                try
                {
                    SLPResult = slp.Send();
                    if (SLPResult != null)
                    {
                        FirstTime = null;
                        TryTick   = 0;
                        return(SLPResult);
                    }
                    else
                    {
                        throw new NullReferenceException("Reply is null");
                    }
                }
                catch (SocketException e)
                {
                    //恢复连接后有两种可能性:
                    //1.服务器崩溃
                    //2.客户端网络异常
                    //这边将来我可能会写更好的处理方法,现在只要崩溃了就无脑清空屏幕和玩家列表(玩家列表在FristPrint那边清理)
                    Screen.Clear();
                    IsFirstPrint = true;
                    if (e.SocketErrorCode == SocketError.HostNotFound)
                    {
                        //我没找到linux上这个错误的错误代码...
                        //这边好像不需要处理了?大概是不会到这边才出现错误的吧?
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("服务器地址错误(找不到这个地址)");
                        if (Platform.IsWindows)
                        {
                            Console.ReadKey(true);
                        }
                        Environment.Exit(-1);
                    }
                    else
                    {
                        PrintTime(ref FirstTime);
                        if (Platform.IsWindows)
                        {
                            Console.Title = $"网络发生了一点错误(qwq不要怕!可能过一会就可以恢复啦)";
                            ColorfullyConsole.WriteLine($"&c错误信息&r:&c{e.Message}&e(&c错误代码&f:&c{e.ErrorCode}&e)");
                        }
                        else
                        {
                            Console.Title = $"发生了网络异常";
                            ColorfullyConsole.WriteLine($"&e详细信息&r:&c{e}");
                        }
                        RetryHandler(ref RetryTime, ref TryTick, MaxTryTick);
                        continue;
                    }
                }
                catch (JsonException)
                {
                    try
                    {
                        return(JsonConvert.DeserializeObject <PingReply>(slp.ToString()));
                    }
                    catch (JsonException je)
                    {
                        IsFirstPrint  = true;
                        Console.Title = string.Empty;
                        Screen.Clear();
                        if (je is JsonSerializationException)
                        {
                            string ErrorJson = SLPResult?.ToString();
                            if (!string.IsNullOrWhiteSpace(ErrorJson) &&
                                ErrorJson.Contains("Server is still starting! Please wait before reconnecting"))
                            {
                                if (TryTick > short.MaxValue)
                                {
                                    Console.WriteLine("这服务器怎么一直在开启中的,怕是出了什么bug了...");
                                    Console.WriteLine($"请把这些信息复制给作者来修bug:{je}");
                                }
                                else
                                {
                                    Console.WriteLine("服务器正在开启中,程序将暂时16秒等待服务器开启...");
                                    Thread.Sleep(1000 * 16);
                                }
                                TryTick++;
                                continue;
                            }
                        }
                        PrintTime(ref FirstTime);
                        ColorfullyConsole.WriteLine("&cjson解析错误&f:&r服务器返回了一个无法被解析的json");
                        if (SLPResult != null)
                        {
                            ColorfullyConsole.WriteLine($"&e无法被解析的json&f:");
                            ColorfullyConsole.WriteLine($"{SLPResult.ToString()}");
                        }
                        ColorfullyConsole.WriteLine($"&e详细信息&r:&c{je}");
                        RetryHandler(ref RetryTime, ref TryTick, MaxTryTick);
                        continue;
                    }
                }
                catch (NullReferenceException nre)
                {
                    StandardExceptionHandler(nre, "发生了异常", FirstTime, RetryTime, TryTick, MaxTryTick);
                    continue;
                }
                catch (Exception)
                {
                    Console.Clear();
                    Console.WriteLine($"Time:{DateTime.Now}");
                    throw;
                }
            }
            return(null);
        }