Thinking route
The physical layer medium physical links to other computers
The data link layer MAC address for communication, but only in the same LAN
Network layer to determine whether a computer in a local area network worldwide with ip
A transport layer to determine a process computer with a port
Organize their own application layer data structure, such as JSON, XML and the like for exchanging data between two applications
network programming
Generally refers to a computer network in the Internet, by a plurality of computers through a network cable or other media linked to each other consisting of
Write network-based applications through a program called the network programming
Learning network programming is to learn to use the network to transmit data to each other with another computer to develop applications that support network communications,
c / s structure
Learning network programming is to be accessed through another computer network data, so inevitably requires at least two computers, stood on one computer to share data and procedures for sharing data, and run on another computer to access the data program of,
Further web browser can access data on the server, known as B / S, which is essentially C / S but the client browser
The ultimate goal of learning network programming is to write applications based on C / S structure
Two computers in order to communicate, must have two basic elements
1. The physical connection media, including cable, radio, optical fiber,
2. Communication protocol
Protocol
It is a mutually agreed set of norms by the sender and the recipient
why you need the agreement
which aims to correct the two sides can resolve data
OSI
Open System Interconnection Reference Model, Open Systems Interconnection reference model for communication, abbreviated as OSI
, OSI whole communication process is divided into seven layers, referred to as OSI seven-layer model
The physical layer
physical connection is established through a physical media
may transmit binary data but a simple binary 1010101 incomprehensible
ethernet provides as follows:
-
A set of electrical signals constitute a data packet, called 'frame'
-
Each data frame is divided into: a header and a data head data of two parts
fixing head comprising :( 18 bytes)
-
Sender / source address of 6 bytes
-
Recipient / destination address, six bytes
-
Data type (Ethernet + type tag), 6 bytes
46 byte data comprising :( minimum, maximum 1500 bytes)
-
Specific contents of the packet
head shortest length + data length = 64 bytes, 1518 bytes maximum, exceeds the maximum limit fragment transmission
ethernet predetermined internet access devices must have the card, the address of the transmitting end and a receiving end refers to the address of the card is that the mac address
mac address: each NIC are fired on a world unique mac address, a length of 48 binary factory, generally indicated by 12 hexadecimal numbers (the first six numbers is the vendor, after six pipelined number)
Each LAN is the network address of a final ip ip is a broadcast address
such as: 192.168.13 0 - 255 range is therefore available: 1-254
router is also a switch
broadcast
The most primitive way of using ethernet, broadcast communication mode, i.e. the basic computer communication by roar
Broadcast Storm
With computer link-layer theory, the world will be able to connect to the same network
but we can not all computer equipment connected to the same switch
broadcast storm: While many computers on the same network at the same time send broadcast will broadcast storm bring down the network
Switch not only responsible for letting computers in the network can communicate with each other, but also to optimize network transmission,
How to optimize it?
When you want before pc1 and pc2 communication
1. The need to know the MAC address of the pc2, it must first be broadcast this information to all computers,
2. This information must be handed over to the switch, and then broadcasted by the switch,
3.pc2 discovery message after receiving the message destination MAC own, to reply to the sender of data,
4. must also respond to the switch, then the switch will record the MAC address corresponding to pc2 networks slogans and stored into its own cache,
Finding a MAC address from the cache pc2 5. When hair give pc2 data,
6. If you find pc2 sent directly to the individual, not the needed radio,
7. If there is no previous broadcast process is repeated
This optimization feature called auto-learning function
Must obtain the MAC address is broadcast for the first time a link to a computer
As long as the link once MAC address was recorded the next time the switch would not broadcast
IP protocol
IP protocol is working in the network layer protocol, full name: Internet Protocol Address, translated into Internet Protocol addresses
ip protocol requires each device to a computer network must be assigned an Ip
address
ip is a logical address is not fixed may vary
There is a logical physical presence of the opposite
-
ip protocol defined address ip address is called, v4 widely used version i.e. ipv4, which specifies the network address is represented by 32-bit binary
-
Range 0.0.0.0-255.255.255.255
-
ip address to the concept of network addresses based on the mac address
-
With the network broadcast address then you can narrow range
-
An ip address written as four decimal numbers generally, for example: 192.168.10.1
-
Network number: identify the subnet
Used to indicate where the LAN
-
-
It is used to indicate the position of the host LAN
Classification IP address:
A category reserved for government agencies
1.0.0.0---126.0.0.0
Class B is assigned to medium-sized companies
128.0.0.0---191.255.0.0
Class C is assigned to anyone in need
192.168.0.1 - 192.168.255.254
A multicast Class D
Class E for experiments
Our computers are usually class C ip, beginning with 192.168, just because a class C anyone can use
The subnet mask is a 32-bit address, for shielding a part of the IP address in network identifier and host identifier distinguish and indicate that the IP address is on the LAN, or on a remote network.
It is a part of all network, host-part 0. For example, IP address, 172.16.10.1, if known network is a front portion 24, the main part 8, the subnet mask is 11111111.11111111.11111111.00000000 written as a decimal is 255.255.255.0.
Simple ip address only identifies the type ip address, ip which does not recognize a subnet of example: 192.168.10.1 and 192.168.10.2 can not determine both the same subnet, because do not know which bits represent the network number, which represent the host number
How to determine whether two ip subnet mask belong to the same subnet
Know "Subnet Mask", we will be able to judge whether any two IP addresses in the same subnet. The two methods are the IP address and subnet mask were the AND operation (two digits are 1, the operation result is 1, and 0 otherwise), and compare the results are the same, and if so, to show that they are in the same sub network, otherwise it is not.
Case: known IP address 172 .16.10.1 and 172 .16.10.2 subnet mask are 255 .255.255.0 , I ask if they are on the same subnet? Both were AND operation with the subnet mask, 172.16.10.1 : 10.1011 million .00010000.00001010.000000001 255255.255.255.0: 11111111.11111111.11111111.00000000 AND operation results obtained network address: 10.1011 million .00010000.00001010.000000001 -> 172.16.10.0 172.16.10.2 : 10.1011 million .00010000.00001010.000000010 255255.255.255.0: 11111111.11111111.11111111.00000000 the AND operation results obtained network address: 10.1011 million .00010000.00001010.000000001 -> 172.16.10.0 result is 172 .16.10.0 , so they are the same subnet.
In summary, the role of IP protocol There are two, one is assigned an IP address for each computer, and the other is to determine which address in the same subnet.
data: up to 65,515 bytes.
Ethernet packets and "data" section, only a maximum of 1500 bytes. Therefore, if the IP packet exceeds 1500 bytes, it will need to be split into several Ethernet data frame transmitted separately.
The origin of ARP protocol:? IP is usually dynamic allocation is a logical address, and data transmission must rely on MAC address, how can it get the MAC address by IP ARP protocol do this requires a
arp protocol functions: send a broadcast packet, the destination hosts mac address
First clear each host ip are known, and by the subnet mask to determine whether the same subnet
Case 1: The host 192.168.1.101 192.168.1.102 access
It is on the same subnet ARP request frame content:
1.FF: FF: FF: FF: FF: FF is a special MAC address in the switch will be seen that this address data is broadcast to all hosts within the network
After receiving the ARP request reply 2.192.168.1.102 own MAC to the source host MAC
3. sender (192.168.1.101) after receipt of reply, MAC address stored mapping relationship to each other will ip the cache for future use
ps: arp -a can view the ARP cache list
Determining the content of the data frame after the MAC address of the other party:
Destination IP switch found not in the current subnet,
1. The switch initiates an ARP request to the other destination IP gateway IP, default gateway host number is 1; the reception side (192.168.111.101) gateway is 192.168.111.1
Sender switch ARP data frame initiated by:
2. Upon request from the other gateway is found ip ip own ARP reply to the request, the switch will inform the sender MAC address,
3. The sender switch, the other side of the gateway and the IP and MAC addresses stored in its own ARP cache,
4. inform the sender (192.168.1.101) of the other gateway MAC address, sender MAC and likewise the other gateway stored in the destination IP mapping relationship, the local ARP cache
At this point the end of the ARP request can begin transmitting data
Subsequent determination of the data frame transmitted after the MAC address of the content:
总结:ARP通过广播的方式来获取MAC地址, 不在同一子网时 ARP得到的时对方网关的MAC地址,数据到达对方网关后,由网关根据IP交给对应的主机,当然对方网关获取主机MAC也是通过ARP
ps:路由器 交换机都可以称之为网关!
通过物理层简历链接通道
通过数据链路层的MAC,可以定位到某个局域网中的某台主机,
通过网络层的IP地址,子网掩码,可以定位到全球范围某一局域网下的某台主机
端口号 ,端口是需要联网的应用程序与网卡关联的编号
传输层功能:建立端口到端口的通信
补充:端口范围0-65535,0-1023为系统占用端口
TCP协议
可靠传输,TCP数据包没有长度限制,理论上可以无限长,但是为了保证网络的效率,通常TCP数据包的长度不会超过IP数据包的长度,以确保单个TCP数据包不必再分割。
TCP之所以可靠,是因为在传输数据前需要三次握手确认建立链接
三次握手:
三次握手的过程实际上实在确认我发的你能收到,你发的我也能收到,从而保证数据传输的的可靠性,
链接是一个虚拟的概念,不实际存在,只要三次握手成功即表示连接建立成功!
问题是三次握手时的确能保障数据传输是可靠的,那么握手后的数据要如何保证传输成功呢?
TCP协议要求在发送数据后,必须接收到对方的回复信息才能确认数据成功发送,如果一段时内没有收到回复信息,会自动重新发送,如果重试的次数过多则表示链接可能已经中断!
四次挥手:
四次挥手的目的是保证双方的数据传输已经全部完成,同样是为了保证数据的完整性
总结
其优点很明显:能够保证数据传输是完整的
缺点:由于每次都需要传输确认信息,导致传输效率降低
场景:多用于必须保证数据完整性的场景,例如文本信息,支付信息等!
UDP协议采取的方式与TCP完全不同,其根本不关心,对方是否收到数据,甚至不关心,对方的地址是否有效,只要将数据报发送到网络,便什么都不管了!
总结
优点:由于不需要传输确认信息,所以传输效率高于TCP协议
缺点:传输数据可能不完整
场景:视频聊天,语音聊天等,不要求数据完整性,但是对传输速度要求较高
应用层功能:规定应用程序的数据格式。
至此一连串高低电压就通过层层协议,变成了我们在应用程序中看到的各种数据