socket连接异常提示,及断开自动重新连接的代码案例:
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);
}
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();
}
}