Home > Back-end >  Chat room system practice
Chat room system practice

Time:09-20

7.2.4 multi-threaded use
In suspended animation last reason is because of CPU resources monopoly, if we can create "suspended animation"
The code USES multi-thread mechanism for, you can have the server constantly read socket port at the same time, the original
Program according to continue to run scheduled way,
To 7.2.3 simple chat system, we hope that the server can receive client sends information for many times, until receiving
To "QUIT" to terminate the connection information,
We can use thread mechanism to realize,
1. Add a thread class of realizing the function for receiving information
In singleServer engineering ServerApp class, add an inner class, the function of the inner class is a custom line
Cheng class, mainly realize the server to the client information receiving work many times, add the black part of the code is to realize arbitrary
Information received death cycle for many times, we put it in the thread of the run () method, through multi-threaded operation mechanism, solve the
The program the phenomenon of suspended animation, in SWT, cannot be updated directly in the thread interface components, must through the way of asynchronous more
New, including appendInformation (String STR) method is to use asynchronous update interface,
The class ReadMessageThread extends Thread {
/* covering Thread class run method, in this method, cycle from the port to read information,
Until read "QUIT", then close the socket */
Public void appendInformation (String STR) {
Final strings str1=STR;
Display. GetDefault (.) syncExec (new Runnable () {
Public void the run () {
TextInformation. Append (str1);
}
});
Public void the run () {
String STR="";
While (true) {
Try {
STR=cin. ReadLine ();//read information
} the catch (IOException e) {
Enclosing appendInformation (" input and output abnormal \ n ");
}
If (STR) equals (" QUIT ")) {//if the "QUIT", close the socket, jump out of the loop
Try {
socket.close();
Enclosing appendInformation (" user connection closed \ n ");
} the catch (IOException e) {
Enclosing appendInformation (" socket closed abnormal \ n ");
}
break;
} else//not QUIT information, will read the information for display
Enclosing appendInformation (" from the client to read the following information: "+ STR +" \ n ");
}
}
}
2. Modify the server "to start listening to" button event handling
In 7.2.3 "to start listening to" button on the server event processing modification, realizing the function of sending and receiving the code with
Thread object to implement part (with black code to modify) :
The original code:
String STR=cin. ReadLine ();
TextInformation. Append (" from the client to read the following information: "+ STR +" \ n ");
New modify code:
/* custom thread object */
ReadMessageThread readThread=new ReadMessageThread ();
/* through the start method, let the thread into runnable */
ReadThread. Start ();
At this point, we'll run a simple chat room server side and client engineering, will be found, the client each time
Can send information on the server side display in real time,
7.2.5 practice
In 7.2.4, we have implemented the server constantly receive information, please on the server side and client engineering
Modifications, realize the following functions:
1. Server is received from the client not to withdraw from the information, send a "server has received from you
Information: XX "information to the client,
2. The server is received from the client's withdrawal from the information, send a "QUIT" message to the client, and shut
Close connection,
3. The client can receive real-time server sends the information, when receives the "QUIT" information, close the connection,
4. If the custom in 7.2.4 thread class requirements outside class, please complete the program changes,

JAVA training work, a great god help me do the 7.2.5 practice, want to hand in today
  • Related