20145239 Du Wenshu "Java Programming" 10th Week study Summary

Source: Internet
Author: User

20145239 Java Programming 10th Week study summary Textbook Learning content summary Java network programming
  • Network programming

    Network programming is the transfer of data between two or more than two devices, such as a computer.
  • Network overview

    1. Computer network overview

    (1) Routers and switches constitute the core computer network, the computer is only the node on this network and control, etc., through the optical fiber, network cable and other connections to connect the device, thus forming a huge computer network.

    (2) The main advantage of the network is sharing: Sharing devices and data, now the most common device sharing devices is the printer.

    (3) IP address: In order to be able to easily identify each device on the network, a unique digital ID for each device in the 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. The IP address may be fixed, such as a variety of servers on the network, or it can be dynamic, such as broadband users using ADSL dial-up internet access.

    (4) Domain name: for example, Sohu.com. An IP address can correspond to multiple domain names, and a domain name can only correspond to one IP address.

    (5) DNS server: The data transmitted in the network, all 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 realization of such a function of the server called a DNS server, that is, popular parlance is called Domain name resolution.

    (6) Port: Allows a computer to run multiple network programs at the same time, and each program corresponds to a unique port on the same computer. 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.

    2. Network Programming Overview

    (1) Network programming is the exchange of data between two or more programs.

    (2) "Request-response" model: one end of the communication sends the data, the other side feeds back the data, and the network communication is based on the model. 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.

    (3) client/server structure: Also known as client/server structure, referred to as C/s structure. Advantages: The client is specially developed, according to the need to achieve a variety of effects; disadvantage: Poor versatility, almost no general purpose, in the actual maintenance, also need to maintain a dedicated client and server side, maintenance of a large pressure.

    (4) Browser/server structure: Also known as browser/server structure, referred to as B/s structure. Advantages: The development of the pressure is small, do not need to maintain the client; disadvantage: The browser limit is relatively large, the performance is not strong, can not perform system-level operation. b/s structure is actually a special C/s structure.

    (5) To Peer program: is a special program, including both client programs, but also contains server-side programs. Use the client program section to connect to other seeds (server side) while using the server side to transfer data to other BT clients.

    (6) Protocol (PROTOCOL): The format of the data when the data is actually exchanged. Because of the different protocol formats between the various network programs, the client program is a dedicated structure.

    3. Network communication mode

    (1) In the existing network, there are two main ways of network communication:

    ·TCP(传输控制协议)方式·UDP(用户数据报协议)方式

    (2) TCP mode: In this way for 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. Important data is typically transmitted using TCP. Because TCP needs to establish a dedicated virtual connection and verify that the transmission is correct, TCP is a bit slower to use, and the amount of data that is transmitted is slightly larger than UDP.

    (3) UDP mode: In this way for network communication, do not need to establish a dedicated virtual connection, transmission is not very reliable, if the sending fails the client is not available. A large number of non-core data are transmitted via UDP.
  • Network Programming Technology

1. Network Programming steps

(1) Client network programming steps

1、建立网络连接:客户端网络编程的第一步都是建立网络连接。在建立网络连接时需要指定连接到的服务器的IP地址和端口号,建立完成以后,会形成一条虚拟的连接,后续的操作就可以通过该连接实现数据交换了。2、交换数据:连接建立以后,就可以通过这个连接交换数据了。交换数据严格按照请求响应模型进行,由客户端发送一个请求数据到服务器,服务器反馈一个响应数据给客户端,如果客户端不发送请求则服务器端就不响应。根据逻辑需要,可以多次交换数据,但是还是必须遵循请求响应模型。3、关闭网络连接:在数据交换完成以后,关闭网络连接,释放程序占用的端口、内存等系统资源,结束网络编程。

(2) server-side network programming steps

监听端口:服务器端属于被动等待连接,所以服务器端启动以后,不需要发起连接,而只需要监听本地计算机的某个固定端口即可。这个端口就是服务器端开放给客户端的端口,服务器端程序运行的本地计算机的IP地址就是服务器端程序的IP地址。 获得连接:当客户端连接到服务器端时,服务器端就可以获得一个连接,这个连接包含客户端的信息,例如客户端IP地址等等,服务器端和客户端也通过该连接进行数据交换。一般在服务器端编程中,当获得连接时,需要开启专门的线程处理该连接,每个连接都由独立的线程实现。 交换数据:服务器端通过获得的连接进行数据交换。服务器端的数据交换步骤是首先接收客户端发送过来的数据,然后进行逻辑处理,再把处理以后的结果数据发送给客户端。简单来说,就是先接收再发送,这个和客户端的数据交换数序不同。其实,服务器端获得的连接和客户端连接是一样的,只是数据交换的步骤不同。当然,服务器端的数据交换也是可以多次进行的。在数据交换完成以后,关闭和客户端的连接。 关闭连接:当服务器程序关闭时,需要关闭服务器端,通过关闭服务器端使得服务器监听的端口以及占用的内存可以释放出来,实现了连接的关闭。

(3) TCP mode is required to establish a connection, the pressure on the server side is larger, and UDP is not necessary to establish a connection, the server side of the pressure is small.

2.Java Network Programming Technology

(1) The basic API related to network programming is located in the java.net package, which contains a basic network programming implementation, which is the basis of network programming. The package contains both the underlying network programming classes and the encapsulated specialized processing classes for web-related processing.

(2) 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.

3.TCP programming

In the Java language, the network programming of TCP is provided with good support, in the actual implementation, the Java.net.Socket class represents the client connection, and the Java.net.ServerSocket class represents the server-side connection. In network programming, the details of the underlying network communication have been implemented in a relatively high package, so when the programmer actually programming, only need to specify the IP address and port number to establish a connection. It is because of this high-level encapsulation, on the one hand simplifies the Java language Network programming difficulty, also makes the Java language network programming cannot penetrate to the network bottom, therefore uses the Java language to carry on the network low-level system programming is very difficult, but because the Java language network programming is relatively simple, Therefore, it has been widely used.

Implementation steps for the client in the Java language:

    • To establish a connection:
Socket socket1 = new Socket("192.168.1.103",10000);Socket socket2 = new Socket("www.sohu.com",80);
    • Data exchange:
OutputStream os = socket1.getOutputStream(); //获得输出流InputStream is = socket1.getInputStream(); //获得输入流
    • To turn off network connectivity:
socket1.close();

Implementation steps for the server in the Java language:

    • Listening Port:
new ServerSocket(10000);
    • Get Connected:
socket = ss.accept();
    • Data exchange:
socket = serverSocket.accept(); //接收客户端发送内容is = socket.getInputStream();
    • To turn off network connectivity:
ss.close();

UDP programming uses two main classes:

    • Datagramsocket: Implement network connections, including client network connections and server-side network connections. Although the UDP mode of network communication does not need to establish a dedicated network connection, but after all, it is necessary to send and receive data, Datagramsocket implementation is the transmitter when sending data, and the role of the listener when receiving data. 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 data encapsulation for transport 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. In fact, the role of the Datagrampacket class is similar to the real letter, in the letter contains the address of the letter sent to the recipient, as well as the content sent, the post office only need to follow the address to pass. When receiving data, the received data must also be processed into an object of type Datagrampacket, which contains information such as the sender's address, port number, and the contents of the data. 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.
This week's code hosting

Learning progress Bar
Lines of code (new/cumulative) Blog volume (Add/accumulate) Learning time (new/cumulative) Important growth
Goal 3500 rows 20 articles 300 hours
First week 120/120 1/1 14/14
Second week 340/460 1/2 14/28
Third week 200/660 1/3 14/42
Week Four 320/980 1/4 14/56
Week Five 280/1260 1/5 14/70
Week Six 478/1738 2/7 16/86
Seventh Week 425/2163 2/9 16/102
Eighth Week 859/3022 3/12 16/118
Nineth Week 4482/7504 2/14 21/139
Tenth Week 1171/8675 2/16 20/159
Resources
    • Java Learning Notes (8th Edition)
    • Java Learning Note (8th Edition) Learning Guide

20145239 Du Wenshu "Java Programming" 10th 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.