Xmpp knowledge preparation for Android interview technical points and xmpp knowledge for Android interviews

Source: Internet
Author: User

Xmpp knowledge preparation for Android interview technical points and xmpp knowledge for Android interviews

When xmpp push and xmpp instant messaging are added to your resume, you can spend more than 1000 oceans. The main use is to use socket for communication, but now the integration framework is generally used for development. However, we still need to understand the basics.

Key knowledge points: I/O Stream and socket basic knowledge


This chapter mainly summarizes the videos explained by Guo Dashen.

The client provides the following functions: sending and receiving messages.
Package com. xiaoxin. client; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java.net. socket; public class socketClient {/*** read our input information */private BufferedReader readClientPrintMsg;/*** read server information */private BufferedReader serverToClientMsg; /*** send message */private BufferedWriter ClientToServerMsg; Private Socket socket; public static void main (String [] args) {// socket client socketClient client = new socketClient (); client. start ();} private void start () {try {socket = new Socket ("127.0.0.1", 9898); ClientToServerMsg = new BufferedWriter (new OutputStreamWriter (socket. getOutputStream (); readClientPrintMsg = new BufferedReader (new InputStreamReader (System. in); serverToClientMsg = new BufferedReader (new I NputStreamReader (socket. getInputStream (); // input String readerConetnt; // send the entered information out while (! (ReaderConetnt = readClientPrintMsg. readLine ()). equals ("bye") {// System. out. println (readerConetnt); ClientToServerMsg. write (readerConetnt + "\ n"); ClientToServerMsg. flush (); String serverToMsg = serverToClientMsg. readLine (); System. out. println (serverToMsg) ;}} catch (IOException e) {e. printStackTrace ();} finally {try {// close the stream socket. close (); ClientToServerMsg. close (); readClientPrintMsg. close (); serverToClientMsg. close ();} catch (IOException e) {e. printStackTrace ();}}}}

Server-side writing server-side implementation functions: sending and receiving messages
Package com. xiaoxin. socketServer; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java.net. serverSocket; import java.net. socket; public class SocketServer {/*** Socket server */private ServerSocket socket; // accept information private BufferedReader clientToServerMsg; // send information private BufferedWriter serverToClien TMsg; private Socket client; public static void main (String [] args) {SocketServer server = new SocketServer (); server. startServer ();} public void startServer () {try {// port 9898 socket = new ServerSocket (9898); System. out. println ("server startup"); // enter the blocking status. Wait for the client to intervene and return a socket object client = socket. accept (); System. out. println ("client" + socket. hashCode () + "connected"); clientToServerMsg = new BufferedReader (new InputStreamReader (clien T. getInputStream (); serverToClientMsg = new BufferedWriter (new OutputStreamWriter (client. getOutputStream (); String readermsg; // one row reads the value while (readermsg = clientToServerMsg. readLine ())! = Null) {System. out. println (readermsg); serverToClientMsg. write ("server reply" + readermsg + "\ n"); serverToClientMsg. flush () ;}} catch (IOException e) {e. printStackTrace ();} finally {try {client. close (); clientToServerMsg. close (); socket. close (); serverToClientMsg. close ();} catch (IOException e) {e. printStackTrace ();}}}}


To be continued
Click to download source code

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.