java Socket的怪异之处
2016-05-05 00:00
501 查看
摘要: 怪异之一:
connect(SocketAddress endpoint):这个方法,尝试连接server端,如果连接不上,就抛出IOException异常。如果连接成功了,就继续执行下一步的代码。
怪异之二:
boolean isConnected() :这个方法的结果仅仅代表这个socket曾经成功连接到了server,但并不表示现在这个时刻它和server仍保持连接状态。
有心的人会发现,即使你已经切断了server和socket之间的连接(比如server down掉了或网络中断了),但是这个函数执行的结果仍旧是true。所以,我认为这个方法并不像我们想象中的那么“管用”。
...
public class ClientAgainConnect implements Runnable {
private boolean isReConn = true;
private static final int DELAY_TIME = 5000;
private Socket mClientSocket = null;
private Handler mHandler;
private int mType = -1;
public void run() {
while (isReConn) {
holdTime(DELAY_TIME);
}
}
public void closeReConn(){
isReConn = false;
}
public ClientAgainConnect(Socket clientSocket,Handler handler,int type) {
this.mType = type;
this.mClientSocket = clientSocket;
this.mHandler = handler;
initiate();
}
private void holdTime(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
sendReConnData();
}
private void sendReConnData() {
try {
if (mClientSocket != null && mClientSocket.isConnected()) {
mClientSocket.sendUrgentData(0xFF);
}
} catch (IOException e) {
mHandler.sendEmptyMessage(mType);
initiate();
}
}
// 启动重连线程
private void initiate() {
new Thread(ClientAgainConnect.this).start();
}
connect(SocketAddress endpoint):这个方法,尝试连接server端,如果连接不上,就抛出IOException异常。如果连接成功了,就继续执行下一步的代码。
怪异之二:
boolean isConnected() :这个方法的结果仅仅代表这个socket曾经成功连接到了server,但并不表示现在这个时刻它和server仍保持连接状态。
有心的人会发现,即使你已经切断了server和socket之间的连接(比如server down掉了或网络中断了),但是这个函数执行的结果仍旧是true。所以,我认为这个方法并不像我们想象中的那么“管用”。
...
public class ClientAgainConnect implements Runnable {
private boolean isReConn = true;
private static final int DELAY_TIME = 5000;
private Socket mClientSocket = null;
private Handler mHandler;
private int mType = -1;
public void run() {
while (isReConn) {
holdTime(DELAY_TIME);
}
}
public void closeReConn(){
isReConn = false;
}
public ClientAgainConnect(Socket clientSocket,Handler handler,int type) {
this.mType = type;
this.mClientSocket = clientSocket;
this.mHandler = handler;
initiate();
}
private void holdTime(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
sendReConnData();
}
private void sendReConnData() {
try {
if (mClientSocket != null && mClientSocket.isConnected()) {
mClientSocket.sendUrgentData(0xFF);
}
} catch (IOException e) {
mHandler.sendEmptyMessage(mType);
initiate();
}
}
// 启动重连线程
private void initiate() {
new Thread(ClientAgainConnect.this).start();
}
相关文章推荐
- java-模拟tomcat服务器
- Linux socket 初步
- java socket 注意的地方
- java socket 注意的地方
- C#基于socket模拟http请求的方法
- 简单的Ruby中的Socket编程教程
- Socket不能选择本地IP连接问题如何解决
- C#之Socket操作类实例解析
- 使用C#来编写一个异步的Socket服务器
- C#使用Socket快速判断数据库连接是否正常的方法
- 科学知识:理解socket
- Android聊天工具基于socket实现
- php与flash as3 socket通信传送文件实现代码
- 解决time_wait强制关闭socket
- C#使用Socket上传并保存图片的方法
- 深入php socket的讲解与实例分析
- Linux网络编程之UDP Socket程序示例
- Linux网络编程之socket文件传输示例
- filezilla Failed to create listen socket on port 21 for IPv4 解决办法
- php socket方式提交的post详解