Considerations for socket communication in Java

Source: Internet
Author: User

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.

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.