Network Protocol between mobile phones and PCs (1)

Source: Internet
Author: User

Network Protocol between mobile phones and PCs (1)

Now we use a computer as the server and a mobile phone as the client to implement communication between the mobile phone and the computer. First of all, it is similar to setting up a client on a mobile phone is similar to setting up a client on a computer. First, we create a server on the computer as follows:

 
 
  1. The package mobile phone communicates with the PC;
  2. Import java. io. IOException;
  3. Import java.net. ServerSocket;
  4. Import java.net. Socket;
  5. Public class Server {
  6. Public void setup (int port ){
  7. Try {
  8. // Create a server
  9. ServerSocket host = new ServerSocket (port );
  10. System. out. println ("communication port" + port + "enabled successfully ");
  11. While (true ){
  12. // Wait for client access
  13. Socket socket = host. accept ();
  14. System. out. println ("accessed ");
  15. // Submit the communication with the client to the thread for processing.
  16. ServerThread st = new ServerThread (socket );
  17. // Enable the thread
  18. St. start ();
  19. }
  20. } Catch (IOException e ){
  21. // TODO Auto-generated catch block
  22. E. printStackTrace ();
  23. }
  24. }
  25. Public static void main (String [] args ){
  26. // Input the port number
  27. New Server (). setup (8888 );
  28. }
  29. }

Next, create a thread to process the input stream and output stream obtained by the socket:

 
 
  1. The package mobile phone communicates with the PC;
  2. Import java. io. DataInputStream;
  3. Import java. io. DataOutputStream;
  4. Import java. io. IOException;
  5. Import java. io. InputStream;
  6. Import java. io. OutputStream;
  7. Import java.net. Socket;
  8. Public class ServerThread extends Thread {
  9. Private Socket socket;
  10. Private DataOutputStream dos;
  11. Public ServerThread (Socket socket ){
  12. This. socket = socket;
  13. }
  14. Public void run (){
  15. // Obtain the output input stream from the network connection socket
  16. Try {
  17. // Input stream
  18. InputStream input = socket. getInputStream ();
  19. DataInputStream dis = new DataInputStream (input );
  20. // Obtain the output stream of the Client
  21. Dos = new DataOutputStream (socket. getOutputStream ());
  22. // Read the bytes sent from the client to the server
  23. While (true ){
  24. Int msgType = dis. readInt ();
  25. Int len = dis. readInt ();
  26. Byte [] bytes = new byte [len];
  27. Dis. readFully (bytes );
  28. String content = new String (bytes, "GB2312 ");
  29. System. out. println ("the client says:" + content );
  30. // This is sent to the client
  31. String replyString = "the server received (" + content + ")";
  32. Bytes = replyString. getBytes ("GB2312 ");
  33. // Write to text 1
  34. Dos. writeInt (1 );
  35. // Write the output stream into the byte length
  36. Dos. writeInt (bytes. length );
  37. // Write into bytes
  38. Dos. write (bytes );
  39. // Refresh
  40. Dos. flush ();
  41. }
  42. } Catch (IOException e ){
  43. // TODO Auto-generated catch block
  44. E. printStackTrace ();
  45. }
  46. }
  47. }

These contents have been mentioned in the previous section, and we will not elaborate on them too much here.

Next we will focus on creating a client in Android. Here we will use eclipse with Android plug-in to compile

First, the interface is processed. I have defined a textview to display the chat content. editText is used as the input box, and a send button is used. This simple chat interface has been completed, as shown in the following figure, readers can beautify the page on their own:

Finally, remember to bind the send button to the specified function.


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.