android java socket断线重连
thread = new Thread(new Runnable()
{
@Override
public void run()
{
while (true){
boolean close = isServerClose(socket);//判断是否断开
if(close){//没有断开,开始读数据;
try
{
Log.d(TAG, "ZYF ip"+getIp());
socket = new Socket(getIp(),10801); //创建Socket实例,并绑定连接远端IP地址和端口
Log.d(TAG,"ZYF SOCKET 创建成功!");
Message msg = new Message();
msg.what = TOAST_TRUE;
handler.sendMessage(msg);
SocketReceiveImgAndVideoFromServer();
}
catch (IOException e)
{
Message msg = new Message();
msg.what = TOAST_FAILE;
handler.sendMessage(msg);
e.printStackTrace();
}
try {
Thread.sleep(5000);//五秒判断一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Looper.prepare();
Looper.loop();// 进入loop中的循环,查看消息队列
千万别乱用这个,这个能让线程堵塞!!!!!!!!!!!!!!!