Android网络多次连接测试,网络正常时出现连接超时失败
2015-05-26 15:46
423 查看
修改前:
public boolean testTcp() {
boolean ret = false;
if (mRouterIp == null || mRouterIp.length() < 10) {
Log.e(TAG, "hecong mRouterIp == Null or .length < 10");
return ret;
}
Socket socket = new Socket();
try {
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(mRouterIp, 80), 2000);
} catch (SocketException e1) {
// TODO Auto-generated catch block
Log.e(TAG, " e1 SocketException");
e1.printStackTrace();
return ret;
} catch (IOException e) {
Log.e(TAG, "e2 SocketException");
// TODO Auto-generated catch block
e.printStackTrace();
return ret;
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
//推测为socket在网络不好时,没有关闭,影响连接.
修改后:
public boolean testTcp() {
boolean ret = false;
if (mRouterIp == null || mRouterIp.length() < 10) {
Log.e(TAG, "hecong mRouterIp == Null or .length < 10");
return ret;
}
Socket socket = new Socket();
try {
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(mRouterIp, 80), 2000);
ret = true;
} catch (SocketException e1) {
// TODO Auto-generated catch block
Log.e(TAG, " e1 SocketException");
e1.printStackTrace();
ret = false;
} catch (IOException e) {
Log.e(TAG, "e2 SocketException");
// TODO Auto-generated catch block
e.printStackTrace();
ret = false;
}
try {
if(socket != null )socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean testTcp() {
boolean ret = false;
if (mRouterIp == null || mRouterIp.length() < 10) {
Log.e(TAG, "hecong mRouterIp == Null or .length < 10");
return ret;
}
Socket socket = new Socket();
try {
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(mRouterIp, 80), 2000);
} catch (SocketException e1) {
// TODO Auto-generated catch block
Log.e(TAG, " e1 SocketException");
e1.printStackTrace();
return ret;
} catch (IOException e) {
Log.e(TAG, "e2 SocketException");
// TODO Auto-generated catch block
e.printStackTrace();
return ret;
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
//推测为socket在网络不好时,没有关闭,影响连接.
修改后:
public boolean testTcp() {
boolean ret = false;
if (mRouterIp == null || mRouterIp.length() < 10) {
Log.e(TAG, "hecong mRouterIp == Null or .length < 10");
return ret;
}
Socket socket = new Socket();
try {
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(mRouterIp, 80), 2000);
ret = true;
} catch (SocketException e1) {
// TODO Auto-generated catch block
Log.e(TAG, " e1 SocketException");
e1.printStackTrace();
ret = false;
} catch (IOException e) {
Log.e(TAG, "e2 SocketException");
// TODO Auto-generated catch block
e.printStackTrace();
ret = false;
}
try {
if(socket != null )socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- android程序连接网络测试:连接失败的原因。
- android网络连接经常出现Timeout,NetworkOnMainThreadException问题,手动浏览器访问web地址正常
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- linux 配置ip重启网络失败 出现未知连接
- SQL 2005服务启动失败(在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误)
- android客户端程序连接服务器总是失败,出现IOException
- android 入门学习笔记 检查网络连接是否正常 判断是否为横屏
- Mcafee 8.0 and foxmail——“出现连接服务器失败,网络繁忙或服务器已关闭”
- Android Https网络连接失败,图片加载不出
- android 判断网络连接、sim卡信息以及ping操作是否网络连接正常
- 【转】android程序连接网络出现android.os.NetworkOnMainThreadExceptionat
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误,用户 'sa' 登录失败
- linux 配置ip重启网络失败 出现未知连接(来源网络只作为个人笔记)
- 网络测试正常连接电脑网卡灯不亮
- android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
- Android测试网络是否连接
- android关于手机连接http接口超时,虚拟机连接正常的原因
- Android出现java.net.SocketException: Permission denied 的问题 ,Android连接网络