20145234 Huangfei "Java Programming" Tenth Week study summary

Source: Internet
Author: User
Tags server port

Summary of the summarization of teaching material learning Contents Network Overview
    • Network programming technology is currently a mainstream programming technology, with the gradual enhancement of networking trend and the large number of network applications, so in the actual development of network programming technology has been a lot of use.
Computer network Overview
    • IP address: In order to be able to easily identify each device on the network, each device in the network will have a unique digital ID.
      • In the computer network, the name IP address is now defined as the IPV4 protocol, which specifies that each IP address consists of a number of 4 0-255, such as 10.0.120.34. Each computer that is connected to the network has a unique IP address, which may be fixed.
      • However, because the IP address is not easy to remember, so for the convenience of memory, there is another concept created-domain name, such as Sohu.com. an IP address can correspond to multiple domain names, and a domain name can only correspond to one IP address. The concept of a domain name can be analogous to the phone's address book, because the mobile phone number is not easy to remember, so add a name identification number, in the actual call can choose the name, and then dial.
      • The data transmitted in the network, all with the IP address as the address identification, so in the actual transfer of data before the need to convert the domain name to an IP address, the implementation of such a function of the server called a DNS server, that is, popular parlance is called Domain name resolution. For example, when a user enters a domain name in a browser, the browser first requests the DNS server, converts the domain name to an IP address, and then feeds the converted IP address back to the browser before the actual data transfer.
    • Ports (port):
      • Each program in the same computer corresponds to a unique port, so that the data sent to each port can be differentiated on a single computer, in other words, multiple network programs can be run concurrently on a single computer without interfering with each other.
      • On the hardware, the port number must be located between 0-65535 , each port is unique to a network program, a network program can use multiple ports.
      • When such a network program runs on a single computation, whether it is a client or a server, it consumes at least one port for network communication. When receiving data, it is first sent to the corresponding computer, and then the computer forwards the data to the corresponding program according to the port.
Network Programming Overview
    • Server and client:
      • In network communication, the first program to initiate the communication is called the client program, which is called the client, and the program waiting for the connection in the first communication is referred to as the server side (server) program, or server. Once the communication is established, the client and server side are exactly the same, without the essential difference.
      • Client/server structure: Client/server structure, also known as client/server structure, referred to as C/s structure.
      • Advantages: The advantages of the structure is that because the client is specially developed, so according to the need to achieve a variety of effects, professional point is expressive rich, and the server side also needs to be developed specifically.
      • Insufficiency: The generality is poor, can not be generalized etc., namely one kind of program client only and the corresponding server side communication, but cannot and other server side communication, in the actual maintenance, also needs to maintain specialized client and the server side, the maintenance pressure is larger.
    • Browser/server structure:
      • Using the browser as the client's structure is called the browser/server structure, also known as the browser/server structure, referred to as B/s structure.
      • Advantage: The advantage of this structure is that the development is less stressful and does not require maintenance of the client.
      • Insufficient: For example the browser limit is relatively big, the expressive force is not strong, cannot carry on the system level operation and so on.
    • Point-to-point program:
      • It is a special program that should include both client and server-side programs in one-to-peer programs.
    • Protocol (PROTOCOL):
      • Computer in the actual exchange of data, in order to let the receiver understand the data, the computer is more stupid, what do not understand, then you need to specify the format of the data, the format of the data is the protocol.
Network communication mode
    • In the existing network, there are two main ways of network communication:
      • TCP (Transmission Control Protocol) mode
      • UDP (User Datagram Protocol) mode
      • For two ways of description: In the network communication, the TCP mode is similar to the call, the use of this way of network communication, the need to establish a dedicated virtual connection, and then reliable data transmission, if the failure to send a message, the client will automatically resend the data. And the UDP way is similar to sends the text message, uses this way carries on the network communication, does not need to establish the specialized virtual connection, the transmission is not very reliable, if sends the failure the client cannot obtain.
Network programming Technology Network programming steps
    • Whether using TCP or UDP for network communication, network programming is made up of both the client and the server side.
    • The implementation is not language-neutral, that is, this step applies to a variety of language implementations, not limited to the Java language.
Client Network Programming steps
    • STEP1: Establishing a network connection
      • The first step in client network programming is to establish a network connection. When establishing a network connection, you need to specify the IP address and port number of the server to which you are connected, and after completion, a virtual connection is formed, and subsequent operations can be exchanged through the connection.
    • STEP2: Exchanging data
      • Once the connection is established, the data can be exchanged through this connection. Exchange data in strict accordance with the request response model, the client sends a request data to the server, the server feedback a response data to the client, if the client does not send the request, the server side will not respond.
      • Depending on your logic needs, you can exchange data multiple times, but you still have to follow the request response model.
    • STEP3: After the data exchange is complete, close the network connection, release the port, memory and other system resources, and end the network programming.
Server-side network programming steps
    • STEP1: Listening port
      • The server side is a passive wait connection, so after the server is started, you do not need to initiate a connection, but you only need to listen to a fixed port on the local computer.
      • This port is the server-side open to the client port, the server-side program runs the local computer's IP address is the server-side program IP address.
    • STEP2: Get Connected
      • When the client connects to the server side, the server can obtain a connection that contains information about the client, such as the client IP address, and the server side and the client also exchange data through the connection.
      • Typically in server-side programming, when you get a connection, you need to turn on a dedicated thread to handle the connection, and each connection is implemented by a separate thread.
    • STEP3: Exchanging data
      • The server side is exchanging data through the connections it obtains. The server-side data exchange step is to first receive the data sent by the client, then the logical processing, and then the processing of the resulting data sent to the client. In simple terms, it is the first to receive and then send, which is different from the client's data exchange sequence.
      • In fact, the server-side connections and client connections are the same, but the data exchange steps are different. Of course, the server-side data exchange can also be done multiple times.
      • After the data exchange is complete, the connection to the client is closed.
    • STEP4: Close Connection
      • When the server program shuts down, the server side needs to be shut down, and the server listening port and memory can be freed by shutting down the server side, enabling the connection to be closed.
Java Network Programming Technology
    • A basic network Class--inetaddress class. The function of this class is to represent an IP address and to include an IP address and domain name-related action method inside the class.
TCP Programming TCP Client programming
    • In Client network programming, you first need to establish a connection, in the Java API, the Java.net.Socket class object represents the network connection, so establish a client network connection, that is, the creation of the socket type object, which represents the network connection, the example is as follows:
      Socket socket = new Socket(“192.168.43.105”,8080);
    • After the connection is established, and then according to the "request-response" model for network data exchange, in the Java language, the data transfer function is implemented by Java IO, that is, only need to get the input stream and output stream from the connection, and then write the data to be sent to the output stream of the connection object, Reading data from the input stream after the send is complete:

      OutputStream os = socket1.getOutputStream();   //获得输出流InputStream is = socket1.getInputStream();     //获得输入流
    • Finally, when the data exchange is completed, the network connection is closed, the system ports and memory resources are freed, and the network operation is completed:
      socket.close();
TCP Server Programming
    • In server-side programming, because the server side of the implementation of the passive waiting for the connection, so the first step of server-side programming is to listen to the port, that is, to listen for a client connection to arrive:
      ServerSocket ss = new ServerSocket(8080);
    • The connection is then obtained when a client connection arrives, establishing a socket connection object corresponding to the client connection, thereby freeing the client connection for server-side port occupancy:
      Socket socket = ss.accept();
    • After the connection is obtained, the subsequent programming is similar to the client's network programming, the socket type of the connection is the same as the network connection of the client, but the server needs to read the sent data first, and then the logical processing later sent to the client, That is, the order in which data is exchanged and the client exchanging data is reversed.
    • Finally, after the server-side communication is complete, close the server-side connection:
      ss.close();
Multiplexing Connection Client
    • Principle: After the connection is established, the logic of the data exchange is written into a loop so that the connection is not closed as long as the loop does not end, and can be implemented in such a way as to establish a connection for multiple data exchange.
Multiplexing Connection Server
    • Principle: According to the logic of the client implementation, can also reuse the server-side connection, the implementation of the principle is also the server-side data exchange logic written in the loop.
Multi-Client Server
    • A server side generally needs to provide communication for multiple clients at the same time, when the server receives a connection, initiates a dedicated thread processing and communication with the client, so that multiple clients can be supported.
    • The Multhreadsocketserver class implements server-side control, implements a receive client connection, and then opens a dedicated logical thread to handle the connection, and the Logicthread class implements the logical processing of a client connection, placing the processed logic in the class's Run method.
UDP Programming Overview
    • The use of UDP does not need to establish a dedicated virtual connection, because there is no need to establish a dedicated connection, so the pressure on the server is much smaller than TCP, but the use of this way is not reliable transmission, so in the network programming, must require reliable transmission of information is generally implemented using TCP, Normal data is implemented using UDP.
    • The UDP network programming also obtains the good support in the Java language, because it does not need to establish the special connection during the transmission data the characteristic, therefore the implementation structure and the TCP method design in the Java API is not quite the same. Of course, the classes that need to be used are still included in the java.net package.
    • In the Java API, the implementation of UDP programming, including client network programming and server-side network programming, mainly by two classes implemented:
      • Datagramsocket:datagramsocket implements the transmitter at the time of sending the data, and the role of the listener when the data is received. Analogous to a network connection in TCP, this class can be used either to implement client connections or to implement server-side connections.
      • Datagrampacket: The object of the class represents the data exchanged in the network. In UDP network programming, whether the data to be sent or the data to be received must be processed into an object of type Datagrampacket, which contains the address sent to, the port number sent to, and the content sent.
UDP Client Programming
    • The UDP way of establishing the connection and the TCP method is different, only need to establish a connection object, do not need to specify the server IP and port number, the example is as follows:
      DatagramSocket ds = new DatagramSocket();
    • When sending data using UDP, it is necessary to first convert the data content that needs to be sent to a byte array, and then construct the data content, server IP, and server port number together into an object of type Datagrampacket, so that the preparation of the data is complete. Sending this object is sent when the Send method in the network connection object is called, and the UDP mode also follows the "request-response" model when the network is communicating.
    • The receive data for UDP is implemented by first constructing a data buffer array that stores the received server-side feedback data, which must be longer than or equal to the length of the actual valid data for server-side feedback. It then constructs a Datagrampacket packet object based on the buffer array, and finally calls the Receive method of the Connection object to accept the data. The received server-side feedback data is stored inside an object of type Datagrampacket.
    • UDP closes the connection, consistent with TCP, using the Close method in the Connection object:
      ds.close();
UDP Server Programming
    • Similar to the previous TCP mode of network programming, the following example is only a functional example of network programming, there are the previous described the client cannot do multiple data exchange, and the server side does not support the problem of multiple clients
Network protocol
    • Network protocol refers to the data format in the network, the essence of which is the client program and server-side program for the data of a convention.
    • The processing that the client program needs to complete is:
      • Generation of the client Send protocol format
      • Parsing of server-side feedback data format
    • The processing that the server-side program needs to complete is:
      • Generation of server-side Feedback protocol format
      • Parsing of the client Send protocol format
Network programming examples
    • Client program features:
      • Receiving user console input
      • Whether the input content is legal
      • Generate send data in protocol format
      • Send data
      • Receive server-side feedback
      • Parse the server-side feedback and output
    • Server-side program features:
      • The receiving client sends the data
      • Parsing data in protocol format
      • Determine if a number is prime
      • Generate protocol data based on the results of the decision
      • Feedback data to the client

20145234 Huangfei "Java Programming" Tenth Week study summary

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.