1. Socket communication in Java
A, blocking waiting
In Java socket communication and IO communication, blocking wait is a very important concept. When a program executes to block program code, such as Serversocket.accept (), Inputstream.read (), it waits until the function has been satisfied with the execution condition to continue execution.
Application:
In IO communication, we can continuously accept the information sent by the socket user.
DataInputStream din=new DataInputStream (Socket.getinputstream ());
while (true) {
String S=din.readutf ();
}
Agree that we are receiving the keyboard input information is the same.
BufferedReader breader=new BufferedReader (New InputStreamReader (system.in));
while (true) {
String S=din.readline ();
}
B, in writing about the application application, we will write the socket and other IO communication program code directly into the application program code, because so that we can be more convenient and interface control interaction.
C, in the socket communication,
First, when the client connects to the server, the server must first respond to the client for a connection success message.
Second, when the client exits, the client must notify the server to disconnect the socket, release the server resources, and release the corresponding thread in the server thread pool.
Finally, if the user closes the window directly, then in the window listener
|
void |
windowclosing (WindowEvent e)
The window is called while it is in the closing process. |
method to release the resource.