Java Network IO Programming Summary (BIO, NIO, AIO all contain complete instance code) __ algorithm

Source: Internet
Author: User

Reprint please indicate the source: http://blog.csdn.net/anxpp/article/details/51512200, thank you.

This article will be from the traditional bio to NiO to the AIO from the shallow to the deep introduction, and enclose the complete code explanation.

This example is used in the following code: The client sends a formula string to the server, and the server evaluates the result to the client.

All the instructions for the code, directly as comments, embedded in the code, see the code is easier to understand, the code will use a calculation of the result of the tool class, see article Code section.

Related basic Knowledge Article recommendation:

Introduction to Linux network I/O models (text)

Java concurrency (multithreading) 1, bio programming
1.1. Traditional bio-programming

The basic model of network programming is the C/S model, which is the communication between two processes.

Server provides IP and listening ports, the client through the connection to the server to listen to the address to initiate a connection request, through three handshake connection, if the connection was successfully established, the two sides can communicate through the socket.

In the traditional development of the synchronous blocking model, ServerSocket is responsible for binding IP address, initiating the listening port, and the socket is responsible for initiating the connection operation. After the connection is successful, both sides communicate through the input and output streams in a synchronous blocking type.

A brief description of the server-side communication model for bio: A server that uses the Bio communication model, typically a stand-alone acceptor thread that listens for client connections, and after receiving a client connection request, creates a new thread for each client after the link processing has not been processed. The thread is destroyed by returning the answer to the client via the output stream. That is, a typical request one answer overnight model.

Traditional bio Communication Model diagram:

The biggest problem with this model is the lack of elasticity and scalability, when the client concurrent access increased, the number of threads on the server side and the number of concurrent access to the client is proportional to 1:1, the Java thread is also a more valuable system resources, the rapid expansion of the number of threads, the performance of the system will be drastically reduced, as the volume of traffic continues to increase , the system will eventually die--off.

Synchronous blocked I/O creation of the server source:

[Java]  View Plain  copy package com.anxpp.io.calculator.bio;   import java.io.ioexception;    import java.net.serversocket;   import java.net.socket;  /**   * bio Server source    *  @author  yangtao__anxpp.com   *  @version  1.0    */   public final class servernormal {        //default port number        private static int DEFAULT_PORT =  serversocket       private  of 12345;       //single case static serversocket server;       //Sets the listening port based on incoming parameters, if no parameters call the following methods and use default values        public static void start ()  throws ioexception{            //use defaults           &Nbsp; start (default_port);       }       // This method will not be a large number of concurrent access, do not need to consider the efficiency, direct method synchronization on the line        public synchronized static  void start (int port)  throws IOException{            if (server != null)  return;            try{               // Creating serversocket               //by constructors If the port is legitimate and idle, the server listens for success                server  = new serversocket (port);                SYSTEM.OUT.PRINTLN ("Server started, port number:"  + port);                 //monitor client connections via wireless loop                 //If there is no client access, it will block on the accept operation.                while (True) {                   Socket  Socket = server.accept ();                    //when there is a new client access, the following code is executed                     //then create a new thread to handle this socket link                     new thread (

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.