Socket connection, to determine the connection interruption, if interrupted, and automatically reconnect.

Source: Internet
Author: User
Tags getmessage

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 ());             }          }    &nbsp}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.