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,就属于这种情况。导致的结果是,即使断网也会判断为有网。
最终解决办法是,自己加个延迟回调,过一段时间连不上网就调用断线重连吧