1. Network Model
A: The network model is the problem of communication between computer networks.
B: Network models generally refer to
OSI (open System interconnection Open Systems Interconnection) Reference Model
TCP/IP Reference Model
C: Network Reference Model Diagram
Network Model 7 Layer Overview:
1. Physical layer: The main definition of physical equipment standards, such as the interface type of network cable, fiber interface type, the transmission rate of various transmission media. Its main function is to transfer the bit stream (that is, from 1, 0 into the current strength to transmit, to the destination after the conversion to 1, 0, that is, we often say that the analog-to-digital conversion and ADC). This layer of data is called a bit.
2. Data Link layer: The Package and solution package of the MAC address (the address of the network card) of the data received from the physical layer mainly. This layer of data is often called a frame. The device at this level is the switch, and the data is transmitted through the switch.
3. Network layer: The main data received from the lower level of the IP address (example 192.168.0.1) encapsulation and solution package. The device at this level is a router that often calls this layer of data a packet.
4. Transport Layer: Defines a number of protocols and port numbers for transmission data (WWW port 80, etc.), such as: TCP (Transmission Control Protocol, low efficiency, reliability, high transmission reliability requirements, large data data), UDP (User Datagram Protocol, contrary to TCP characteristics, For transmission reliability requirements are not high, small data data, such as QQ chat data is transmitted in this way. The main purpose is to segment and transmit the data received from the lower level, and then reorganize it after reaching the destination site. This layer of data is often called a segment.
5. Session Layer: A path for data transmission through the Transport Layer (port number: Transport port and receive port). Initiate or accept session requests primarily between your systems (devices need to know each other to be IP or Mac or hostname)
6. Presentation layer: Mainly for the interpretation of the received data, encryption and decryption, Compression and decompression (i.e., the things that a computer can recognize can be converted to something that an adult can recognize (such as pictures, sounds, etc.).
7. Application layer: Mainly some terminal applications, such as FTP (a variety of file downloads), the WEB (ie browsing), QQ and so on (it can be understood as we can see on the computer screen things.) is the terminal application).
2. Three elements of network communication
A:ip Address: inetaddress
The identification of the device in the network, not easy to remember, available host name
B: Port number
Used to identify the logical address of a process, identification of different processes
C: Transport protocol
Rules for communication
Common agreement: TCP,UDP
3.IP Address
IP address = network number + host address
Class A IP address: the first paragraph number is the network number, the remaining three paragraph number is the local computer number
Class B IP Address: the first two paragraph number is the network number, the remaining two paragraph number is the local computer number
Class C IP Address: The first three segment number is the network number, the remaining part of the number is the local computer number
Special Address:
127.0.0.1 loopback address, which can be used to test whether there is a problem with the local network. Ping 127.0.0.1
DOS command ipconfig: view native IP address
xxx.xxx.xxx.0 Network Address
xxx.xxx.xxx.255 Broadcast Address
Class A 1.0.0.1-127.255.255.254
(1) 10.x.x.x is a private address (private address is not used on the Internet, but is used in the local area network address)
(2) 127.x.x.x is reserved address, used for cyclic testing.
Class B 128.0.0.1-191.255.255.254 172.16.0.0-172.31.255.255 is a private address. 169.254.X.X is a reserved address.
Class C 192.0.0.1-223.255.255.254 192.168.x.x is a private address
Class D 224.0.0.1-239.255.255.254
Class E 240.0.0.1-247.255.255.254
4.InetAddress: This class represents an Internet Protocol (IP) address.
This class constructs the method, then how to make the function that the class provides.
A class without a constructor method:
A: The members are all static (Math,array,collections)
B: Single case design mode (Runtime)
C: A static member method in the class returns the object of the class (public static InetAddress Getbyname (String host) uses this method)
To master the function
Get any host: public static inetaddress Getbyname (String host): Determines the host's IP address in the case of a given host name or a string of IP addresses.
Get host Name: Public String gethostname ()
Get host IP Address: public String gethostaddress ()
5. Port
Physical Port network card port
Logical ports We're talking about logical ports.
A: Each network program will have at least one logical port
B: Used to identify the logical address of the process, identification of different processes
C: Valid port: 0~65535, where the 0~1024 system uses or retains the port.
6. Agreement
Udp:
Pack the data
Data is limited (packet max 64k)
Non-connection oriented
Not rely on
Fast speed
Examples: Chat messages, online video, video conferencing, texting, post office parcels.
Tcp:
Connection-oriented (three-time handshake to ensure connection creation)
Data unrestricted
Safe and reliable
A little less efficient
Example: Download, call, QQ chat
7.socket: Network socket, network programming
Socket sockets: IP address + port number
Socket principle Mechanism:
There are sockets at both ends of the communication.
Network communication is actually the communication between the sockets.
Data is transmitted through IO between two sockets.
UDP protocol sends data
A: Create A send-side Socket object (Datagramsocket)
B: Create the data and package the data
C: Invoke the Send method of the socket object send packet Send (Datagrampacket p)
D: Freeing Resources close ()
UDP protocol receives data
A: Create A Receive-side socket object Datagramsocket
B: Create a packet (Accept container) Datagrampacket
C: The Receive method that calls the socket object receives packet void receive (Datagrampacket p)
D: Parsing Packet Datagrampacket
InetAddress getinetaddress (): Returns the IP address of a machine
Byte[] GetData (): Returns a data buffer.
int GetLength (): Returns the length of the data that will be sent or received.
E: Releasing Resources datagramsocket->close ()
TCP protocol sends data
A: Create a Socket object socket for the sender
This step, if successful, means the link is successful.
B: Get output stream, write Data OutputStream writes ()
C: Release Resources close ()
The connection is rejected and the TCP protocol must first open the server.
TCP protocol receive data
A: Create a socket object on the receiving end ServerSocket
B: Listens for client connections, returns a corresponding socket object socket accept ()
C: Get input Stream, reading data InputStream
D: Freeing resource sockets-> close ()