After the Android-socket server is disconnected and restarted, the android client automatically reconnects to the androidsocket server.

Source: Internet
Author: User

After the Android-socket server is disconnected and restarted, the android client automatically reconnects to the androidsocket server.

I have been studying this issue for a whole day! I finally came out, but I didn't have much sense of accomplishment. Why? This is the result of my senior student's work. I did not think of the same problem! It's amazing ......

But I still want to share with you the hope to help you!

First, let me explain: I didn't leave the source code, because this function is a small part of my project. The source code is too powerful, and it may not be usable after download, so paste the core code ~

I send a heartbeat to the server every minute to maintain the connection, as shown below:

Code in sendObject () (that is, sending content to the server ):

Public void sendObject (String obj) throws IOException {DataOutputStream OS = new DataOutputStream (socket. getOutputStream (); OS. write (obj. getBytes (); OS. flush (); System. out. println ("Send:" + obj + "client ");}

The point is, how can we ensure automatic connection when the service is restarted?

Public static void resetSocket () {while (SocketClient. isServerClose (socket) {try {socket = new Socket (SERVERIP, PORT);} catch (UnknownHostException e) {// TODO Auto-generated catch block/e. printStackTrace ();} catch (IOException e) {System. out. println ("re-connecting .... "); // TODO Auto-generated catch block // e. printStackTrace () ;}}/ *** determines whether to disconnect. If yes, true is returned. If no value is returned, false * @ param socket * @ return */public static Boolean isServerClose (Socket socket) is returned) {try {socket. sendUrgentData (0); // sends 1-byte emergency data. By default, the server does not enable emergency data processing and does not affect normal communication. return false;} catch (Exception se) {return true ;}}

Finally, put SocketClient. resetSocket (); into the catch {} in the image, and reconnect when an exception is caught to achieve the desired effect.

Hope to help you. Thank you!

Related Article

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.