20145310 Java programming 10th Week of study summary

Source: Internet
Author: User

20145310 "Java program Design" The 10th Week study summary textbook Learning content Summary

Network overview

Network programming is the transfer of data between two or more than two devices, such as a computer. What the programmer is doing is sending the data to the specified location, or receiving the specified data, which is the narrow network programming category. When sending and receiving data, most programming languages design specialized APIs to implement these functions, and programmers only need to invoke them.

Each device in the network will have a unique digital ID, which is the IP address. 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

To facilitate 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.

Port: A unique port for each program on the same computer, which can be used to differentiate the data sent to each port on a single computer, that is, a computer can run multiple network programs concurrently (on the hardware, the port number must be located between 0-65535)

Network programming is the exchange of data between two or more programs, compared with ordinary single-machine programs, the biggest difference in network programs is the need to exchange data programs run on different computers, which results in the complexity of data exchange.

A DNS server is a server that converts a domain name to an IP address before it actually transmits data, and when the user enters the domain name in the 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.

The port is for a computer to be able to run multiple network programs at the same time, each program in the same computer corresponds to a unique port, so that a computer can be on the port to distinguish the data sent to each port, in other words, a computer can run multiple network programs concurrently, Without interfering with each other.

Server-side network programming steps

Listening Port: The server side is passive waiting for the connection, so after the server is started, you do not need to initiate the connection, but only need to listen to a fixed port on the local computer.

Get connected: When the client connects to the server side, the server can get a connection that contains information about the client, such as the client IP address, and so on, and the server side and the client also exchange data through the connection.

Exchanging data: The server side is exchanging data through the connections obtained. Receives the data sent by the client, then carries on the logic processing, then sends the processing result data to the client. After the data exchange is complete, the connection to the client is closed.

Close connection: When the server program shuts down, the server side needs to be shut down, the port that the server listens to and the memory that consumes can be freed up by shutting down the server side, which realizes the close of the connection.

There are two common structures in Network programming: C/S structure and b/s structure. c/S structure is the client/server structure, in the development of the client and server, the advantages of this structure is due to the client is specifically developed, can be implemented according to the needs of various effects, its shortcomings in the universality of check, maintenance pressure. b/S structure is browser/server structure, in the development of only need to develop server-side, the advantages of this structure lies in the development of the pressure is small, do not need to maintain the client, but the limitations of the viewer is relatively large, not strong expression.

Peer program is a special program, it should be a peer to the program contains both client programs, as well as server-side programs, such as BT, using the client program part of the other seed (server side), and use the server side to the other BT client transfer data.

The code to implement server-side snooping is:
ServerSocket ss = new ServerSocket (10000);

The implementation of UDP programming, including client network programming and server-side network programming, mainly by two classes implemented, respectively:
Datagramsocket: Implement network connections, including client network connections and server-side network connections. The Datagramsocket implements the transmitter when the data is sent, 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: Implements the encapsulation of data that is transmitted over the network, and objects of that class represent 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. Compared with the TCP network transmission, IO programming is not necessary in the network programming of UDP, and the structure is simpler than the TCP mode of network programming.
InetAddress class: The function of this class is to represent an IP address, and the IP address and domain name-related action methods are included inside the class.

UDP programming

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:

A network protocol is defined as the data format that is transmitted over a network. The essence of the network protocol is a convention of the client program and the server-side program to the data. In the design of the general follow the "simple, universal, easy to parse" principle.
Typically involves two network protocol formats: The client sends a data format and the server-side feedback data format. The processing that the client program needs to complete is:
(1) Generation of the client Send protocol format
(2) Analysis of server-side feedback data format
(3) The processing that the server-side program needs to complete is:
(4) Generation of server-side Feedback protocol format
(5) Parsing of the client sending protocol format
Because various network programs use different protocol formats, it is not possible to generalize between clients of different network programs.

Client Send protocol format:

Converts a user-entered number to a string, and then converts the string to a byte array.

For example, user input 16 is converted to the string "16" and converted to a byte array using GetBytes.

The client sends a "quit" string representing the end connection

Server-side Send protocol format:

The feedback data has a length of 1 bytes. The number 0 stands for Prime, 1 is not prime, and 2 represents the protocol format error.

For example, the client sends the number 12, then feedback 1, send 13 feedback 0, send 0 feedback 2.

Other (sentiment, thinking, etc., optional)

Knowledge of network programming is necessary for us, and it is necessary to master it well.

Code hosting:

Learning progress Bar /Cumulative) new/cumulative)
lines of code (newBlog volume (Learning time (new/cumulative) Important growth
Goal 5000 rows 30 Articles 400 hours
First week 100/100 2/2 10/10 Initial knowledge of Java
Second week 233/250 1/3 12/22 Mastering Java Basic Syntax
Third week 637/787 2/4 20/42 Recognize objects, object encapsulation
Week Four 500/1287 1/5 20/62 Inheritance and polymorphism, interface and polymorphism
Week Five 300/1587 1/6 20/82
Week Six 300/1887 2/8 20/102
Seventh Week 300/2187 2/10 20/122
Eighth Week 410/2500 2/12 20/142
Nineth Week 710/3233 2/14 20/162
10th Week 210/3345 2/16 20/162
Resources
      • Java Learning Notes (8th Edition)
      • Java Learning Note (8th Edition) Learning Guide

20145310 Java programming 10th Week of 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.