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:
- The package mobile phone communicates with the PC;
- Import java. io. IOException;
- Import java.net. ServerSocket;
- Import java.net. Socket;
- Public class Server {
- Public void setup (int port ){
- Try {
- // Create a server
- ServerSocket host = new ServerSocket (port );
- System. out. println ("communication port" + port + "enabled successfully ");
- While (true ){
- // Wait for client access
- Socket socket = host. accept ();
- System. out. println ("accessed ");
- // Submit the communication with the client to the thread for processing.
- ServerThread st = new ServerThread (socket );
- // Enable the thread
- St. start ();
- }
- } Catch (IOException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
- Public static void main (String [] args ){
- // Input the port number
- New Server (). setup (8888 );
- }
- }
Next, create a thread to process the input stream and output stream obtained by the socket:
- The package mobile phone communicates with the PC;
- Import java. io. DataInputStream;
- Import java. io. DataOutputStream;
- Import java. io. IOException;
- Import java. io. InputStream;
- Import java. io. OutputStream;
- Import java.net. Socket;
- Public class ServerThread extends Thread {
- Private Socket socket;
- Private DataOutputStream dos;
- Public ServerThread (Socket socket ){
- This. socket = socket;
- }
- Public void run (){
- // Obtain the output input stream from the network connection socket
- Try {
- // Input stream
- InputStream input = socket. getInputStream ();
- DataInputStream dis = new DataInputStream (input );
- // Obtain the output stream of the Client
- Dos = new DataOutputStream (socket. getOutputStream ());
- // Read the bytes sent from the client to the server
- While (true ){
- Int msgType = dis. readInt ();
- Int len = dis. readInt ();
- Byte [] bytes = new byte [len];
- Dis. readFully (bytes );
- String content = new String (bytes, "GB2312 ");
- System. out. println ("the client says:" + content );
- // This is sent to the client
- String replyString = "the server received (" + content + ")";
- Bytes = replyString. getBytes ("GB2312 ");
- // Write to text 1
- Dos. writeInt (1 );
- // Write the output stream into the byte length
- Dos. writeInt (bytes. length );
- // Write into bytes
- Dos. write (bytes );
- // Refresh
- Dos. flush ();
- }
- } Catch (IOException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
- }
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.