A recent problem in development is how to tell if a remote server is disconnected, and if it is disconnected, it needs to be reconnected.
First think of the socket class method isclosed (), isconnected (), Isinputstreamshutdown (), Isoutputstreamshutdown (), but after testing and viewing the relevant documents, These methods are local-side states and cannot be used to determine whether the remote is disconnected.
Then think of whether you can send a piece of test data through OutputStream, if the sending failure means that the remote has been disconnected, like Ping, but this will affect the normal output data, the remote can not separate the normal data and test data.
Finally returned to the socket class, found that there is a method sendurgentdata, see the document that it will send a byte of data to the output stream, as long as the other side of the socket So_oobinline property does not open, it will automatically discard this byte, and So_ The Oobinline property is turned off by default, which is great, just what I need.
As a result, the following code can be used to determine whether a connection is disconnected at the remote end:
try{Socket.sendurgentdata (0xFF);} catch (Exception ex) {reconnect ();}
————————————————————————————————————————————————
Oh. The following is the listening connection program I wrote:
/** * Listener Connection Threads * */class monitorconnect extends thread{ private SocketStatus ss = null; private String address = null; private int Port = 0; public monitorconnect (socketstatus ss,string Address,int port) { this.ss = ss; this.address = address; this. port = port;    &NBSP} public void run () { while (true) { if (Ss.getsocket () == null) {      &NBSp; socketaddress socketaddress = new inetsocketaddress (Address,port); try{ socket socket = new socket (); socket.connect (socketaddress,5000); boolean status = true; while (status) { try{ socket.sendurgentdata (0xFF); ss.setsocket (socket); thread.sleep (1000); }catch (exception e1) {   SYSTEM.OUT.PRINTLN ("Server disconnected. "); ss.setsocket (NULL); status = false; } } }catch (exception e) { system.out.println ("Connection failed, reconnect after 3 seconds" + E.getmessage ()); } } try{ thread.sleep (3000); }catch (Exception e) { system.out.println ( E.getmessage ()); } }  }}