Dark Horse programmer ---- java basics-network programming (1)
I. Network Reference Model
1. OSI reference model and TCP/IP Reference Model
Layer-7 description:
(1). Physical Layer: mainly defines physical device standards, such as the interface type of the network cable, the interface type of the optical fiber, and the transmission rate of various transmission media. It is mainly used to transmit bit streams.
(2). Data Link Layer: encapsulate and unencapsulate the data received from the physical layer to the IP address of the MAC address Nic. The working device of this layer is a switch, and data is transmitted through the switch.
(3). Network Layer: encapsulate and unencapsulate the data received in the lower layer, for example, 192.168.0.1. This layer works on a router ..
(4 ). transmission Layer: defines some data transmission protocols and port numbers, such as WWW port 80), mainly refers to the data received from the lower layer for segmentation and transmission, to the destination address before restructuring.
(5). Session Layer: Build a data transmission path through the transport layer port number: Transmission port and receiving port. It is mainly used to initiate a session between your systems or receive a session request between devices that need to know each other, either an IP address, a MAC address, or a host name ).
(6 ). presentation Layer: mainly refers to interpreting the received data, encryption and decryption, compression and decompression, that is, converting things that can be recognized by computers into things and sounds that can be recognized by adults ).
(7 ). application Layer: Mainly some terminal applications, such as FTP file downloads), WEBIE browsing), QQ, and so on can be understood as what we can see on the computer screen, is the terminal application ).
Note:
(1). the MAC address of each Nic is globally unique.
(2) The router sends data packets to the specified location.
(3) The process of communication between applications is the process of packaging and unpacking between layers.
(4) the OSI reference model, although well-designed, is too cumbersome and inefficient. Therefore, a simplified TCP/IP reference model is generated.
2. Three elements of network communication: IP address, port number, and transmission protocol.
(1) IP Address: InetAdderss class
1. It is the identifier of the device in the network.
2. It is hard to remember and can be expressed by the host name. There is a ing between the two.
3. The number of IPV4 addresses is not enough, so IPV6 is generated.
4. local loopback address: 127.0.0.1 Host Name: localhost
5. In the absence of an Internet connection, a default IP address, that is, the local loopback address, is assigned to access the local computer for convenience.
(2) Port Number
1. The logical address used to identify the process application) and the identifier of different processes
2. Valid ports: 0 ~ 65535, 0 ~ 1024 the system uses or retains the port.
3. No program has a port number for setting it as an application.
(3) transmission protocol
This is the communication rule. Common protocols include UDP and TCP.
UDP
1. encapsulate data and source and target data packets without establishing a connection
2. The size of each data packet is limited to 64 KB.
3. Because there is no connection, It is an unreliable protocol.
4. No need to establish a connection, fast
Application Case: QQ chat, video chat, walkie talkie, FeiQ, video teaching, and other online videos are all UDP
TCP
1. Establish a connection to form a channel for data transmission
2. transfer large amounts of data in the connection
3. It is a reliable protocol that completes the connection through three handshakes. Three-way handshake: the first request sent by the local party, the second request confirmed by the other party, and the third request confirmed by the other party that the connection was successful.
4. You must establish a connection to reduce the efficiency.
Application Cases: FTP file transmission, thunder download, and telephone
3. Common network methods of java.net. InetAdderss
No constructor. Only methods can be used to return objects of this class.
- Static InetAddress getLocalHost (); // returns an InetAdderss Class Object
- Static String getHostName (); // returns the Host Name of the IP address.
- Static String getHostAddress (); // returns the IP address String
- String toString (); // returns the String representation of this IP address.
- Static InetAddress getByName (String host); // obtain the IP address of the specified host name
- Static InetAddress [] getAllByName (String host); // obtain all IP addresses of the specified host name
Code Demonstration:
- Import java.net .*;
- Class InetAddressDemo
- {
- Public static void main (String [] args) throws Exception
- {
- InetAddress ia = InetAddress. getLocalHost (); // returns the InetAddress object.
- String name = ia. getHostName (); // obtain the Host name of the IP address
- String ip = ia. getHostAddress (); // obtain the local ip Address
- System. out. println ("name =" + name );
- System. out. println ("ip =" + ip );
-
- InetAddress I = InetAddress. getByName ("192.168.1.1"); // obtain the IP address of the specified host.
- System. out. println (I. getHostAddress ());
-
- InetAddress [] ina = InetAddress. getAllByName ("www.sina.com.cn"); // obtain all IP addresses of the specified host name
- For (InetAddress a: ina)
- {
- System. out. println (a. getHostAddress ());
- System. out. println (a. getHostName ());
- }
- }
- }