Android uses multithreading for network chat room communication

TCP / IP communication protocol is a reliable network protocol. It establishes a socket at both ends of the communication, so as to form a network virtual link between the two ends of the communication. Once the virtual network link is established, the programs at both ends can communicate through the virtual link. Java provides a good package for network communication based on TCP protocol. Java uses socket object to represent the communication interfaces at both ends, and generates IO flow through socket for network communication.

The following program demo is to implement a simple C / S chat room application. Each client contains two threads: one is responsible for generating the main interface, responding to user actions, and writing the user input data into the output stream corresponding to the socket; The other is responsible for reading the data in the input stream corresponding to the socket (data sent from the server) and displaying these data on the program interface. The client program is an Android application, so you need to create an Android project. The interface of this Android application contains two text boxes: one is used to receive user input; The other is used to display chat messages. There is also a button in the interface. When the user clicks this button, the program sends chat information to the server. layout/activity_ The main.xml interface layout code is as follows:

The activity of the client is responsible for generating the program interface, binding the event listener for the button click event of the program, and sending information to the server when the user clicks the button. The logic code of mainactivity.java is as follows:

When the user clicks the "send" button in the program interface, the program will send the contents in the input box to the revhandler object of clientthread, which is responsible for sending the contents entered by the user to the server.

The clientthread sub thread is responsible for establishing a connection with the remote server and communicating with the remote server. After reading the data, it sends a message through the handler object; When the clientthread sub thread receives the message sent by the UI thread, it is also responsible for sending the user input to the remote server.

The clientthread.java logic code is as follows:

The function of the above thread is also very simple. It just continuously obtains the content in the socket input stream. After reading the content in the socket input stream, it sends a message through the handler object, which is responsible for carrying the read data. In addition, the sub thread is also responsible for reading the message sent by the UI thread. After receiving the message, the sub thread is responsible for sending the data carried in the message to the remote server.

The server side should contain multiple threads. Each socket corresponds to one thread. This thread is responsible for reading the input stream corresponding to the socket and sending the read data to each socket output stream. Therefore, it is necessary to use list on the server side to save all sockets. The following is the server-side code. The program provides two classes for the server: one is to create the main class of ServerSocket listening; The other is the thread class responsible for handling each socket communication.

/Multithreadserver / SRC / myserver.java logic code is as follows:

The above program is that the server side is only responsible for receiving the connection request of the client socket. Whenever the client socket connects to the ServerSocket, the program adds the corresponding socket to the socketlist set and saves it, and starts a thread for the socket. The program is responsible for handling all communication tasks of the socket. The code of the server-side thread class is as follows.

/Multithreadserver / SRC / serverthread.java logic code is as follows:

The above server-side thread class constantly reads the client data. The program uses the readfromclient() method to read the client data. If an IOException exception is caught during data reading, it indicates that there is a problem with the client socket corresponding to the socket, and the program deletes the socket from the socketlist. After the server thread reads the client data, the program traverses the socketlist set and sends the data to each socket in the socketlist set once -- the server thread will forward the data read from the socket to each socket in the socketlist once.

First run the Myserver class of the above program. After this class runs, it only acts as a server and does not see any output. Then you can run the Android client - equivalent to starting the chat interface to log in to the server. Next, after any Android client enters some content, click the "send" button to see that all clients (including themselves) will receive the content just entered. In this way, you can simply realize the function of a C / S structure chat room. Note: since the program needs to access the Internet, it also needs to authorize the permission to access the Internet in the manifest file androidmanifest.xml:

The screenshot of demo program running effect interface is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>