Android interview technical points: xmpp knowledge preparation Socket server termination, Android xmpp

Source: Internet
Author: User

Android interview technical points: xmpp knowledge preparation Socket server termination, Android xmpp

The previous section has basically completed communication between the server and the client, but does not implement persistent connection communication. This section fixes the bug.


1. for client code writing, we need to enable a thread to wait for the server to send a message.
public void startServerReplyListener(final BufferedReader serverToClientMsg){new Thread(new Runnable() {@Overridepublic void run() {try {String responce;while ((responce=serverToClientMsg.readLine())!=null) {System.out.println(responce);}} catch (IOException e) {e.printStackTrace();}}}).start();}


2. server-side code writing is actually impossible for all servers to serve a single client. Then, as long as the client is connected, we will not connect the client again, so the code should be changed, use a while loop to loop together and wait for client connection
// Enter the blocking status, wait for the client to intervene, and return a socket object while (true) {client = socket. accept (); ManagerConnection (client );}

Then enable thread reply Information
Public void ManagerConnection (final Socket socket) {new Thread (new Runnable () {@ Overridepublic void run () {try {System. out. println ("client" + socket. hashCode () + "connected"); clientToServerMsg = new BufferedReader (new InputStreamReader (client. getInputStream (); serverToClientMsg = new BufferedWriter (new OutputStreamWriter (client. getOutputStream (); String readermsg; // one row reads the value while (readermsg = clientToServerMsg. r EadLine ())! = Null) {System. out. println (readermsg); System. out. println ("client" + socket. hashCode () + ":" + readermsg); serverToClientMsg. write ("server reply" + readermsg + "\ n"); serverToClientMsg. flush () ;}} catch (IOException e) {e. printStackTrace ();} finally {try {clientToServerMsg. close (); serverToClientMsg. close ();} catch (IOException e) {e. printStackTrace ();}}}}). start ();}
End
To be continued
Click to download source code

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.