Unity3D查看网络状态的N种姿势

17 篇文章 1 订阅
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using System.Net;

public class Main : MonoBehaviour
{
    public Button btu;
    public Text txt;


    public Button btu1;
    public Text txt1;

    public Button btu2;
    public Text txt2;

    public Button btu3;
    public Text txt3;

    public Button btu4;
    public Text txt4;
    //bool isLocalAreaConnected = NetworkInterface.GetIsNetworkAvailable();

    // Start is called before the first frame update
    void Start()
    {
        btu.onClick.AddListener(delegate {
            Function1();
        });
        btu1.onClick.AddListener(delegate {
            Function2();
        });
        btu2.onClick.AddListener(delegate {
            Function3();
        });
        btu3.onClick.AddListener(delegate {
            Function4();
        });
        btu4.onClick.AddListener(delegate {
            Function5();
        });
    }
    void Function1()
    {
        txt.text = (NetworkInterface.GetIsNetworkAvailable() ? true : false) + "  ";
    }

    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

    void Function2()
    {
        int I = 0;
        bool state = InternetGetConnectedState(out I, 0);
        txt1.text = (state ? true : false) + "  ";
    }

    void Function3()
    {
        bool _is = false;
        try
        {
            HttpWebRequest re = (HttpWebRequest)WebRequest.Create("http://sim.download.yofijoy.com/hsn/sim/TD_Chief/");
            HttpWebResponse res = (HttpWebResponse)re.GetResponse();
            if (res.ContentLength != 0)
            {
                _is = true;
                //MessageBox.Show("文件存在");  return true;               
            }
        }
        catch (Exception)
        {
            //Response.Write("不存在");
        }
        txt2.text = (_is ? true : false) + "  ";
    }

    void Function4()
    {
        txt3.text = (IsExist("http://sim.download.yofijoy.com/hsn/sim/TD_Chief/") ? true : false) + "  ";
    }

    void Function5()
    {
        txt4.text = (UrlExistsUsingSockets("150.158.179.167") ? true : false) + "  ";
    }

    public static bool IsExist(string uri)
    {
        HttpWebRequest req = null;
        HttpWebResponse res = null;
        try
        {
            req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "HEAD";
            req.Timeout = 100;
            res = (HttpWebResponse)req.GetResponse();
            return (res.StatusCode == HttpStatusCode.OK);
        }
        catch
        {
            return false;
        }
        finally
        {
            if (res != null)
            {
                res.Close();
                res = null;
            }
            if (req != null)
            {
                req.Abort();
                req = null;
            }
        }
    }

    //3:
    private bool UrlExistsUsingSockets(string url)
    {
        if (url.StartsWith("http://")) url = url.Remove(0, "http://".Length);
        Debug.Log(url);
        try
        {
            System.Net.IPHostEntry ipHost = System.Net.Dns.GetHostEntry(url);// System.Net.Dns.Resolve(url);
            return true;
        }
        catch (System.Net.Sockets.SocketException se)
        {
            System.Diagnostics.Trace.Write(se.Message);
            return false;
        }
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A)) Function1();
        if (Input.GetKeyDown(KeyCode.B)) Function2();
        if (Input.GetKeyDown(KeyCode.C)) Function3();
    }

   
   
}



unity自己也提供有网络状态判断的API

if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
{
    //2G/3G/4G
}
else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
{
    //wifi
}
else if (Application.internetReachability == NetworkReachability.NotReachable)
{
    //duan wang
}

但是!但是!但是!

如果再某些特殊情况下,手机端是缓存有socket数据的,也就是说即使你断网的情况下,也会判断为有网。而以上几种方式,基本属于各有利弊。

我们一个策划的极光 VPN,就属于这种情况。导致的结果是,即使断网也会判断为有网。

最终解决办法是,自己加个延迟回调,过一段时间连不上网就调用断线重连吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值