一、java socket 判断服务端已经断开连接或者被其他用于连接的方法,直接上代码。
public class Daemon_App_Thread extends Thread { boolean host_reachable = false; public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(check_connect_status && CURRENT_CAMERA_STATUS == CAMERA_IDLE) { host_reachable = Utils.sendPingRequest(ip_address, 2000); try { clientSocket.sendUrgentData(0xFF);//clientSocket.setKeepAlive(true); } catch (IOException e) { e.printStackTrace(); host_reachable = false; Log.i(TAG, "The server may be used by other users or offline! "+e.toString()); } if(!host_reachable){ Message msg = mHandler.obtainMessage(); msg.what = CONNECT_ERROR_CLOSE_SOCKET_MESSAGE; msg.obj = "The server may be used by other users or offline!"; mHandler.sendMessage(msg); check_connect_status = false; } } } } } public static boolean sendPingRequest(String ipAddress , int timeout_ms) { try { InetAddress geek = InetAddress.getByName(ipAddress); if (geek.isReachable(timeout_ms)) return true; } catch (IOException e) { e.printStackTrace(); } return false; } publi