java判断远端是否断开了连接:
try{ socket.sendurgentdata(0xff); }catch(exception ex){ reconnect(); }
用ping实现
package com.csdn.test; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class test { static bufferedreader bufferedreader; public static void main(string[] args) throws ioexception { try { process process = runtime.getruntime().exec("ping 192.168.1.104");//判断是否连接的ip; bufferedreader = new bufferedreader(new inputstreamreader(process .getinputstream())); string connectionstr = ""; while ((connectionstr = bufferedreader.readline()) != null) { system.out.println(connectionstr); } } catch (ioexception e) { e.printstacktrace(); } finally { bufferedreader.close(); } } }
ping的方法有个严重的bug,就是你只能判断对方是否连接网络,而不能判断客户端是否开启。
更多java知识请关注java基础教程栏目。
以上就是java判断socket是否断开的方法的详细内容。
