当前位置:网站首页>The Ping class of unity uses
The Ping class of unity uses
2022-06-25 22:59:00 【Smart_ zy】
1. Purpose
1.1 Get ready ping The Internet , See if you can ping through ,ping Pass means that the network link is normal
2. Reference resources
2.1
Unity And Ping Class analysis & Try to use _ Chen yanbixing's blog -CSDN Blog
3. Be careful
3.1 It is not easy to use ,
- Obviously can ping through , But it shows that ping no
- There are no variables in it whether ping Tong ,
- ping_zhuJi.time=-1 ms , In fact, computers can ping through ,

The code is as follows
- ping_zhuJi.time Mainly with this and 0 Compare and judge whether it is linked
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 【Function:ping The Internet 】【Time:2022 06 20】【Author:XZY】
/// </summary>
public class PingNet : MonoBehaviour
{
/// <summary> The host ip, The purpose is to ping Is it open </summary>
[HideInInspector]
//public string ip_zhuJi = "192.168.1.111";
public string ip_zhuJi = "";
/// <summary>Ping object : The host </summary>
Ping ping_zhuJi=null;
/// <summary>Ping object : Backstage </summary>
Ping ping_server;
/// <summary> coroutines : Check whether it hasn't been for a long time ping through , If representative ping no </summary>
private Coroutine coroutine_isPingSuccess=null;
/// <summary>ping One time </summary>
private float waitForSeconds_IE_isPingSuccess = 5;
/// <summary>ping The number of failures , With the process waitForSeconds_IE_isPingSuccess It matters </summary>
private int numPingFail = 0;
/// <summary>ping How many times are failures </summary>
private int totalPingFail = 3;
public static PingNet instance = null;
//public int NumPingFail { get => numPingFail; set => numPingFail = value; }
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
private void Start()
{
//coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());
//StartCoroutine(IE_GetIp_zhuJi());
//StartCoroutine(IE_StartPing());
}
//IEnumerator IE_GetIp_zhuJi()
//{
// yield return new WaitForSeconds(5);
// string _ip = SmartPlayerAndroidMono.Instance.ip_zhuJi;
// Debug.Log("PingNet Address : Get the entire address of the host : " + _ip);
// string[] str_add = _ip.Split(':');
// for (int i = 0; i < str_add.Length; i++)
// {
// Debug.Log("PingNet Address : Get the host's ip: The first " + i + " A field :" + str_add[i]);
// }
// string str_add2 = str_add[1].Trim('/');
// Debug.Log("PingNet Address : Get the host's ip: " + str_add2);
//}
//IEnumerator IE_StartPing()
//{
// yield return new WaitForSeconds(6);
// string _ip = ip_zhuJi;
// if (_ip == "")
// {
// Debug.Log("PingNet: Address : The host ip It's empty , Please check ");
// yield return null;
// }
// else
// {
// ip_zhuJi = _ip;
// }
// if (ping_zhuJi != null)
// {
// ping_zhuJi.DestroyPing();
// ping_zhuJi = null;
// }
// ping_zhuJi = new Ping(ip_zhuJi);
// if (coroutine_isPingSuccess != null)
// {
// StopCoroutine(IE_isPingSuccess());
// coroutine_isPingSuccess = null;
// }
// coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());
//}
/// <summary>
/// 【Function: Get the host's ip】【Time:2022 06 21】【Author:XZY】
/// </summary>
string GetIp_zhuJi()
{
string _ip = SmartPlayerAndroidMono.Instance.ip_zhuJi_all;
Debug.Log("PingNet Address : Get the entire address of the host : " + _ip);
string[] str_add = _ip.Split(':');
for (int i = 0; i < str_add.Length; i++)
{
Debug.Log("PingNet Address : Get the host's ip: The first " + i + " A field :" + str_add[i]);
}
string str_add2 = str_add[1].Trim('/');
Debug.Log("PingNet Address : Get the host's ip: " + str_add2);
return str_add2;
}
/// <summary>
/// 【Function: Start ping】【Time:2022 06 20】【Author:XZY】
/// </summary>
public void StartPing()
{
string _ip = GetIp_zhuJi();
if (_ip == "")
{
Debug.Log("PingNet: Address : The host ip It's empty , Please check ");
return;
}
else
{
ip_zhuJi = _ip;
}
if (ping_zhuJi != null)
{
ping_zhuJi.DestroyPing();
ping_zhuJi = null;
}
ping_zhuJi = new Ping(ip_zhuJi);
if (coroutine_isPingSuccess != null)
{
StopCoroutine(IE_isPingSuccess());
coroutine_isPingSuccess = null;
}
coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());
}
/ <summary>
/ 【Function: send out ping】【Time:2022 06 20】【Author:XZY】
/ </summary>
//void SendPing()
//{
// ping_zhuJi = new Ping(ip_zhuJi);
//}
/// <summary>
/// 【Function: coroutines : whether ping success 】【Time:2022 06 20】【Author:XZY】
/// </summary>
/// <returns></returns>
IEnumerator IE_isPingSuccess()
{
while (true)
{
//Debug.Log(" Address :IE_isPingSuccess:");
yield return new WaitForSeconds(waitForSeconds_IE_isPingSuccess);
Debug.Log("PingNet Address :null != ping_zhuJi:" + (null != ping_zhuJi));
Debug.Log("PingNet Address :ping_zhuJi.isDone:" + ping_zhuJi.isDone);
// Only through delayTime Of <0 Determine whether ping through , if <0 That is, no ping through ; conversely ping through
if (null != ping_zhuJi && ping_zhuJi.isDone)
{
float delayTime = ping_zhuJi.time;
if (delayTime <= 0)
{
numPingFail++;
Debug.Log("PingNet Address :ip_zhuJi:" + ip_zhuJi + ":ping The number of failures :" + numPingFail);
Debug.Log("PingNet Address :Ping: I haven't had it for a long time ping through ");
// break link UI Show
ConnectTipPanel.instance.AddConnectFail();
}
else
{
Debug.Log("PingNet Address :ping_zhuJi:" + ip_zhuJi + ":" + delayTime.ToString() + " ms");
numPingFail = 0;
Debug.Log("PingNet Address :ip_zhuJi:" + ip_zhuJi + ":ping success ");
// break link UI hide
ConnectTipPanel.instance.ConnectSuccess();
}
}
ping_zhuJi.DestroyPing();
ping_zhuJi = null;
ping_zhuJi = new Ping(ip_zhuJi);
}
}
}
边栏推荐
- 2022-2028 global TFT touch screen industry research and trend analysis report
- HotSpot JVM 「01」类加载、链接和初始化
- 2022-2028 global SiC igniter industry research and trend analysis report
- Research Report on China's new energy technology and equipment market competition analysis and marketing strategy suggestions 2022-2028
- ORACLE - 数据查询
- 【EOSIO】EOS/WAX签名错误 is_canonical( c ): signature is not canonical 问题
- Talk about adapter mode
- CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
- 使用EAST ocr遇到的坑解决方法(编译lanms)
- How to open a futures account safely at present? Which futures companies are more reliable?
猜你喜欢

CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
![[intensive lecture] 2022 PHP intermediate and advanced interview questions (II)](/img/50/c9572beaa035d5e6e78a57fcd1302c.jpg)
[intensive lecture] 2022 PHP intermediate and advanced interview questions (II)

Basic concepts of processor scheduling

2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report

Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?

2022-2028 global horizontal reciprocating compressor industry research and trend analysis report

Zhihu Gaozan: what ability is important, but most people don't have it?

2022-2028 global SiC igniter industry research and trend analysis report

万亿热钱砸向太空经济,真的是一门好生意?

Glory launched the points mall to support the exchange of various glory products
随机推荐
Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
MySQL数据库常用函数和查询
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
Analysis report on demand and investment forecast of global and Chinese flame retardant hydraulic oil market from 2022 to 2028
民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系
China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
哪些PHP开源作品值得关注
ES6 -- 形参设置初始值、拓展运算符、迭代器、生成函数
Why is BeanUtils not recommended?
China soft magnetic material market demand status and prospect scale forecast report 2022-2028
Why can't the mobile phone be used and the computer be connected
Jz-064- maximum value of sliding window
Eureka core ⼼ source code analysis
ADB common commands
Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
The new version of Tencent's "peace elite" is coming: add a new account security protection system, and upgrade the detection of in-game violations
Trillions of hot money smashed into the space economy. Is it really a good business?
2022年河南省第一届职业技能大赛网络安全项目试题