A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
Textbook Learning Content Summary Java network programming
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 (Transmission Control Protocol) mode · UDP (User Datagram Protocol) mode
(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.
1. Network Programming steps
(1) Client network programming steps
1. Establish 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.
2, Exchange data: After the connection is established, you can exchange data 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.
3. Close the network connection: After the data exchange is complete, close the network connection, release the port, memory and other system resources, and end the network programming.
(2) 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. 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. 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. 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. Exchanging data: The server side is exchanging data through the connections obtained. 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. 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.
(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.
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.
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.
20145307 10-Week Java Learning report
Start building with 50+ products and up to 12 months usage for Elastic Compute Service