如何在JavaScript中检测Internet连接是否脱机?
我想测试我的应用程序的低网络连接情况.除了站在电梯里,最好的办法是什么?我试过用铝箔包裹我的手机,但它没有多大帮助.
我需要在真实设备上测试它,而不是在模拟器中测试它.
如何检测Android上的网络连接类型?
是通过ConnectivityManager.getActiveNetworkInfo().getType()
,并且答案仅限于Wifi和移动?
我想编写一个脚本,该脚本将继续检查网络中的任何设备是否应该全天在线.我试过用ping,但是
if [ "`ping -c 1 some_ip_here`" ]
then
echo 1
else
echo 0
fi
Run Code Online (Sandbox Code Playgroud)
给人1
如果我输入有效或无效的IP地址不管.如何检查特定地址(或更好的IP地址列表中的任何设备)是否脱机?
我正在创建一个应用程序,用户将频繁的信息和文件上传到我的服务器.这是通过专用的上传器服务在新线程中完成的.
我从这个帖子中知道
你可以相对容易地检查是否有互联网连接.您还可以获取socketTimeoutExceptions以检测Internet连接问题.所有这一切都很好,并且当连接因任何原因无法正常工作时,我可以轻松地缓存上传内容.
我的问题是如何知道何时重新上传?连接恢复时是否触发了事件?或者我是不是在制作一个睡眠的新线程,然后每30秒检查一次互联网连接?
任何想法,将不胜感激!
我通过向导将amazon VPC设置为"仅公共网络",因此我的所有实例都在公共子网中.
VPC中分配了弹性IP的实例可以毫无困难地连接到Internet.
但没有弹性IP的实例无法连接到任何地方.
互联网网关存在.aws控制台中的路由表看起来像
Destination Target
10.0.0.0/16 local
0.0.0.0/0 igw-nnnnn
Run Code Online (Sandbox Code Playgroud)
和实例内部的路线显示
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
default 10.0.0.1 0.0.0.0 UG 100 0 0 eth0
Run Code Online (Sandbox Code Playgroud)
我尝试将所有入站和出站流量打开到实例所属的安全组中的0.0.0.0/0.仍然没有成功.
~$ ping google.com
PING google.com (74.125.224.36) 56(84) bytes of data.
^C
--- google.com ping statistics ---
6 packets transmitted, 0 received, 100% packet loss, time 5017ms
Run Code Online (Sandbox Code Playgroud)
我还可以做些什么?
我已经有了这个代码来监听连接变化 -
public class NetworkStateReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Log.d("app","Network connectivity change");
if(intent.getExtras() != null)
{
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED)
{
Log.i("app", "Network " + ni.getTypeName() + " connected");
}
}
if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE))
{
Log.d("app", "There's no network connectivity");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此代码检查Internet连接 - Internet Check
但问题是,如果网络在没有任何连接变化的情况下突然失去互联网连接,则此代码无用.有没有办法为Internet连接更改创建Broadcast Receiver侦听器?我有一个Web应用程序,突然的Internet连接更改可能会导致问题.
我有这个Java程序: MySQLConnectExample.java
import java.sql.*;
import java.util.Properties;
public class MySQLConnectExample {
public static void main(String[] args) {
Connection conn1 = null;
Connection conn2 = null;
Connection conn3 = null;
try {
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null)
System.out.println("Connected to the database test1");
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个连接到服务器的应用程序.到目前为止,如果服务器可用,登录和数据传输工作正常.服务器不可用时会出现问题.在这种情况下,该方法发送登录请求并等待响应.
有谁知道如何检查服务器是否可用(可见)?
必须实现的简单逻辑的伪代码如下:
我需要可靠地检测设备是否具有完全的互联网访问权限,即用户不仅限于强制网络门户(也称为围墙花园),即有限的子网强制用户在表单上提交凭据以便获得满访问.
我的应用程序正在自动执行身份验证过程,因此在开始登录活动之前知道完全互联网访问不可用非常重要.
问题不在于如何检查网络接口是否已启动且处于连接状态.它是关于确保设备具有不受限制的互联网访问,而不是沙盒内部网段.
到目前为止我尝试过的所有方法都失败了,因为连接到任何知名主机都不会抛出异常但返回有效的HTTP 200
响应代码,因为所有请求都被路由到登录页面.
以下是我尝试的所有方法,但它们都返回true
而不是false
出于上述原因:
1:
InetAddress.getByName(host).isReachable(TIMEOUT_IN_MILLISECONDS);
isConnected = true; <exception not thrown>
Run Code Online (Sandbox Code Playgroud)
2:
Socket socket = new Socket();
SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(host), 80);
socket.connect(sockaddr, pingTimeout);
isConnected = socket.isConnected();
Run Code Online (Sandbox Code Playgroud)
3:
URL url = new URL(hostUrl));
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setRequestMethod("GET");
httpConn.connect();
responseCode = httpConn.getResponseCode();
isConnected = responseCode == HttpURLConnection.HTTP_OK;
Run Code Online (Sandbox Code Playgroud)
那么,我如何确保连接到实际主机而不是登录重定向页面?显然,我可以从我使用的'ping'主机检查实际的响应体,但它看起来不是一个合适的解决方案.
connectivity ×10
android ×6
networking ×3
java ×2
ajax ×1
amazon-ec2 ×1
amazon-vpc ×1
bash ×1
connection ×1
events ×1
http ×1
javascript ×1
jdbc ×1
mysql ×1
offline ×1
ping ×1
redirect ×1
sql ×1
vpc ×1
wifi ×1