The first article of the C + + series: Network article

Source: Internet
Author: User
Tags control characters ming domain name server domain server

Mr. Hu Shi once said: "Publication is a weapon of absorption", so I wrote this article. I looked at some of the basics of the computer, and I felt there was no energy in it, so I wanted to use my way of working the computer correctly, succinctly, and interestingly. This article applies to people who don't have a computer base but want to know about computers and some computer basics but lack a holistic understanding of computers. background

Xiao Ming Learning C + + is actually a story. One day, Xiao Ming had a whim and suddenly taught himself computer. He heard that C + + is a good computer language, so he opened the Computer browser, entered a Web site: http://www.runoob.com/cplusplus/cpp-tutorial.html, and then the browser appeared in the following interface:;
In fact, this is usually with our browser browsing information, to see the video is no different; then, from Xiaoming in the browser to enter the Web site, to the final display of the novice site of C + + tutorials, during this period exactly what happened. Basic Knowledge

Let's take a look at this web site first:

The original web site's professional term is called the Uniform Resource Locator URL (uniform Resource Locator), which is used on the internet to differentiate between documents and resources.
So this URL contains three parts:
(1) The protocol used: here the Hypertext Transfer Protocol (hypertext Transfer Protocol) is used, which is an application layer protocol.

There are two nouns (protocols and application tiers) that may need to be explained that the protocol is a collection of rules, because if a computer communicates with a computer in a different language, it must be unified in one language, and that language is the HTTP protocol. We all know how to use this agreement, so we can communicate. and application layer is a level of computer network layering thought. Layered thinking is a very important thought, because the Internet is very large, so communication is a very complicated thing, in order to make the division of labor more clear, people put network network into several levels, which is the highest level of application layer, this layer is responsible for the implementation of specific network applications, including mailboxes Ah, Internet ah, watch video Ah, Pass the file, and so on.

(2) Host domain name: www.runoob.com, this is the domain name of the rookie website. Popularly said is the name of the website, make people more easy to remember. For example, Www.baidu.com is the name of Baidu website. Relative to the domain name, is the IP address, we know that the IP address is to identify a specific computer. In general, the IP address and the domain name has a corresponding relationship, we in the Windows system cmd Ping command can be traced to a domain name corresponding to the IP address:

The figure shows that the www.runoob.com corresponding IP address is 183.61.180.185.
then why do I need a domain name? Because the domain name is good to remember AH.
Why should there be an IP address? Because the domain name is often irregular, the length is different. Some domain name is very long, some domain name is very short, to the computer network routing part, it is not easy to find a road to the destination computer, so use IP to find a way. We will also explain in detail how to find a way based on IP.
(3) Pathname: cplusplus/cpp-tutorial.html, which is where to get resources from the novice site host. The resources here are cpp-tutorial.html. The suffix name HTML lets you know that this is a file written in Hypertext Markup Language HTML. HTML is a markup language, for example, if I want to display an article, I directly use a different tag to show that I want to display the caption here, show the subject under the heading, and then insert a picture in a part of the body. The left side of the illustration on the

is the HTML code, and the right side is the actual result of the browser; the head/p/img of the head are all different tags. First step: Find the IP address for this domain name

Xiao Ming is lazy, leaving only a Web site, the rest of the work to the computer to do their own.
The computer also spares no effort, dedication. It knows, want to contact the rookie website, let it send the C + + tutorial, first need to find its IP address. So how to find it.
There is a specialized department (composed of computers) on the network to do this, that is, domain Name System DNS. The
DNS is specifically responsible for conversion from domain names to IP addresses. In this system, it is made up of servers. A server can be understood as a computer that specializes in providing a service, such as a DNS server that provides domain names to IP translation services.
Within this system is divided into different roles, the root domain server is the highest level of domain name server, it knows all top-level domain name server domain name and IP, the top-level domain name server is responsible for managing the top-level domain name server under All level two domain names, the right domain name server in charge of a zone. In addition, there is a local domain name server, which is the default domain name server, it may be inside a college, a university or the same network provider ISP.
This system process is probably like this:
(1): First ask the local domain name server: Hey, you have www.runoob.com IP address ah.
in the Win7 IPV4 option, you can set this local domain name server:

The image above shows that the IP address of the local domain name server is 202.96.134.133. If you are the first time to visit the novice website, the local domain name server does not know the novice site's address. Will come to the second step. If the corresponding IP address is found, then the process of looking for an IP address is happily over.
(2) Local domain name server to root domain server for help: Big guy, I have a domain name resolution request, you know its IP. The root domain server discovers that this URL is followed by the. com end, and it tells The local server the address of the top-level domain name server in charge of. com.
(3) The local server is responsible for. com top-level domain server, rookie site IP is how much. Top-level domain name server found two domain name is Runnoob, and then in the system to find in charge of the Runoob server. The server address is then handed to the local domain name server.
(4) The local server then asks the right domain name server, and the right domain name server says yes, 183.61.180.185.
(5) The local Domain name server reports DNS resolution results to the host.
The following diagram is the flowchart of the above process:

Finally, I found the address of the rookie website.

If Xiaoming continues to visit the rookie website in the near future, it needn't be so troublesome. Because the computer is not stupid, the local server will save the results of this resolution, the next time to look up the IP address directly through the local domain name server returned. You do not need to query the root domain server. Step Two: The browser sends HTTP requests to the server

As mentioned earlier, the HTTP protocol is a Hypertext Transfer protocol at the application layer. As the name suggests, this protocol is used to transmit hypertext files with the. html suffix name.
So what does an HTTP request message contain?
We can use a grab tool wireshark to get the HTTP request message:

The above illustration shows:
(1) The request line uses the Get method to indicate the meaning of obtaining the resource. In addition, post (send form), update (update), delete (delete), put, and other methods. The Get method indicates that the browser wants to obtain an HTML document about the C + + tutorial.
(2) connection for the keep-alive, in order to simplify the design of the server, HTTP protocol is stateless, that is, the server does not know who visited me, when the visit, as well as the number of visits. and the lower layer of the HTTP protocol is the TCP protocol, and the TCP protocol is based on reliable transmission of the connection (as will be said below). In the http1.0 version, the connection is automatically disconnected for each resource that is transferred. But every time a connection is built, it consumes resources, so keep-alive stays connected for a while and saves resources.
(3) The role of cookies: Because of the stateless HTTP, so that the server can not know the user access information. Therefore, a similar token must be used between the user and the server so that whenever the user lights out the token, the server will know that it is you so that the access information can be logged. Cookies are such a thing.

Thus, when the HTTP request message is constructed, it is handed over to the TCP protocol at the transport layer. Step Three: The browser establishes a TCP connection with the server

TCP is powerful and responsibility is significant.

In my own understanding, TCP is a reliable end-to-end connection, and in order to avoid some of the network's temporary failures, increased retransmission and congestion control functions.
Here's what the end refers to.
The end here refers to a socket. We make a running program into a process (for example, the open browser is a process), then the socket is meant to represent a unique process. The socket contains two parts: IP and port number. If a building is compared to a computer, then a room is a port number. Courier must be sent to the room to complete the task, simply sent to the door of the building not yet.

A TCP connection can be established by using a socket consisting of an IP address and a port number.
TCP connections are established to provide a reliable and error-free channel for the application layer to transmit data.

The establishment and release of TCP connections

The image above looks so complex that there seems to be some unnecessary operation, so why not send the data directly. In fact, all the steps are designed to ensure reliable transmission standards.
(1) TCP three handshake: the first three steps above is three times handshake. Why do you shake hands three times? Two times is all right. No. To give an extreme example, Xiaoming's computer shuts down after sending the first message. In other words, although this message is sent by Xiaoming's computer, but after a period of time, this message has been invalidated. If the server receives this message that the computer has just been sent, send a reply to it and establish a connection. But the server waits and waits, waits for not to arrive each other to send the message over. So wasted the server resources.
(2) Data transfer: After establishing a connection, both parties (should refer to the browser process and the server's service process) can send data to each other. The browser randomly selects a port on the local computer, combining the local IP address to form a socket, while the server listens to the port (Web service default port) and waits for the client's request. When the 80 port received the data, we know that there is a request to come, and then send a response message based on the content of the message back.
(3) Four waves: Finally, everyone sent out, in order to send a message sent a signal, when the signal received, must return confirmation signal. The connection is disconnected. Why it takes four times. Because the other person may have data sent to me, so I need to send an end signal to each other. Why do I need confirmation? To prevent the other person from staying connected.

The following figure is a TCP message that is crawled through Wireshark:

which includes its own port: 33224 and the other side of the port: 80;
In addition, there are serial numbers, confirmation number, window size, inspection and other fields, these are for retransmission and congestion control, interested in you can find information to learn about the TCP protocol, here is not detailed. Fourth step: IP routing

The next layer of the transport layer is the network layer. The network layer is responsible for finding a way to the destination computer.
In the search for the road, IP packets will encounter a lot of "referring to passers-by", these "refers to passers-by" is the router.
The role of routers is to route.

The TCP message becomes the network layer data, the network layer gives the TCP message header plus the local IP address and the destination host's IP address, then becomes the IP message.

The following figure shows the format of the IP message:

The version number 4 indicates the use of IPv4, in addition to the length of the head, the length of the package, checksum, local and destination IP address.

the topological structure of a campus network

Now we assume that Xiaoming is in a student dormitory, he uses the computer network is the campus network.

Then xiaoming used the computer will first connect to the dormitory switch. A switch is a data link layer device, it is a bit similar to the socket, you can plug a lot of network cable. Computers that connect the same switch can surf the Internet at the same time.

Then exchange the opportunity to connect to the Campus Network center of the core routers, generally we will be called gateways.

Then Xiaoming's computer wants to pass with other computers, will first reach the gateway, the gateway will find the destination IP address is inside or outside the net. If the intranet can communicate directly. If it is an extranet, it needs to be forwarded to another router, which is forwarded until the last router directly connected to the destination host. The router then sends the packet to the destination host. (In fact, this also shows that the local area network internal computer communication is relatively fast, external communication is slower, because the external need to be limited by routers and bandwidth, so the speed of the internet slowed down)

The above process is an approximate process. The details are detailed below.
There are three key points, including Nat, ARP, and routing options.
(1) NAT
We know that the IP address is represented by a 32-bit binary. It's a lot of computers that can be said by reason. However, with the development of the Internet, these IP addresses are running out, so someone thought of a way. Is the IP address multiplexing chant.
Specify that the address of the range is a private address that does not have to be circulated on the public web.
10.0.0.0~10.255.255.255 (Class A)
172.16.0.0~172.31.255.255 (Class B)
192.168.0.0~192.168.255.255 (Class C)

So the question came, my intranet computer to do the Internet.
The idea is to come out of people. The answer is NAT, which is address translation technology. Its process is as follows:

(1) intranet host 192.168.1.3 need access to server 1.1.1.2. First it sends a local ip:192.168.1.3 IP packet with the destination IP as 1.1.1.2.
(2) After the gateway, it is to use NAT technology to turn the local IP into the public network IP of the router port, that is, the local ip:20.1.1.2, the purpose is ip:1.1.1.2; This requires that the translation be recorded with the NAT address translation table:

(3) to the server, the server returned the response message, to the gateway, to find the NAT conversion table, found the corresponding intranet IP address.
(4) The Gateway forwards the response message to the local host.

This completes the process of accessing the extranet. In addition, there are napt, is to use the port number and public network IP address to access the external network, the advantage is that when the intranet has a small number of public network IP can also allow enough to host the Internet.

(2) route selection
Before routing, routers need to run routing algorithms to obtain routing information.
About the routing algorithm can refer to my blog: routing algorithm
After the operation of the routing algorithm, the router will have the corresponding routing table, the local host also has, open cmd Input command route print, get the following results:

According to this routing table, if you want to access the IP address of the Novice Web site, 183.61.180.185, go to find this routing table, if not found in the default route (the first of the above routing table) reached the gateway, and then the gateway based on its own routing table to find, The lookup is routed according to the longest matching principle, or the longest matching route item is selected. In this way, IP packets passed through a number of routers, eventually reached the novice Web site host.
Use the TRACERT command to see which routers have been reached:

The principle of the tracert command is this: it sends a series of IP datagrams from the source host to the destination host, and the datagram encapsulates UDP datagrams that cannot be delivered. The TTL of the first datagram is set to 1 and the second is set to 2, so that each time the TTL becomes 0, the source host is sent an ICMP timeout error message. The last data is reported to the destination host, but the TTL is not reduced to 0, but the content format cannot be delivered by UDP datagram, so the destination host sends an ICMP endpoint unreachable message to the source.

(3) ARP address translation
First you have to understand what is the MAC address (hardware address), the MAC address is a 48-bit global unified physical address. Although the hardware of interconnected networks varies widely, they all use the same physical address format. Then the next layer of data link layer in the network layer, not using the IP address, but using physical address for transmission.
Some people may ask, why not use the IP address directly. Because it is more convenient to use an IP address. In this world, there are a variety of networks, they use different hardware addresses, to enable them to communicate with each other, need to use very complex hardware address conversion work. But the advent of IP solves the problem. Because using IP, it's like communicating on the same network.

Use the command arp-a in CMD to view the address Translation table for the local computer:

You can see that the IP address and MAC address have a one-to-one mapping relationship.

So how does this really work?
Take a look at the picture below:

Now assume that host a needs to know that the IP address is 192.168.1.2 MAC address, then it will want to broadcast ARP requests inside the LAN. This allows each host to receive an ARP request. But receive requests each host will be more than the IP address is its own IP address (A is not looking for me.) ), only the matching IP address will respond. Host B matches exactly, so he propagates his MAC address to host a alone.
So what if it's a different LAN. Different LAN can not use a broadcast on the completion of ARP conversion. It then sends an ARP request to the local gateway first. Found the hardware address of the gateway after the IP datagram sent to the gateway, when the gateway sent an ARP request, to find the next router's hardware address, so, finally found a different LAN destination host MAC address. Step Fifth: encapsulate the frame into a bit stream

Data in the network layer is encapsulated into IP packets, is the data link layer and the physical layer of things.
Data link layer is mainly concerned with a local area network (Campus network) inside a host and another host (Xiaoming's computer and gateway router) how to transmit data. Instead of focusing on the Internet as IP protocols do.

The above image is a kind of Ethernet frame content, can see the content contains the source physical address and destination physical address, type, data and so on content.
The data link layer mainly needs to solve three problems:
(1) How do I identify the start and end of a frame.
In Ethernet, it is a bit more than a special transmission, then must know where is the beginning, where is the end, in order to a bit stream as a whole. If you are transferring ASCII characters, you can identify a frame with two control characters Soh (0x1) and EoT (0x4), and when the sender reads this, you know, oh, it's a frame.
(2) When the data I want to transfer contains the start of the frame or the terminator.
You can use the escape character. When the data contains Soh or EOT, insert an escape character (this character can optionally be selected) to identify this is not the bounds of the frame.
(3) There is no error in the transmitted data. What to do if there is a mistake.
When a bit error occurs, you can use CRC cyclic calibration (This is a calibration method). If there is an error, you can discard this frame. You can also report to the upper level protocol.
So the data link layer's work is done.

Then give it to the physical layer.
The physical layer needs to specify whether to use fiber or twisted-pair or radio to transmit data, the value of the level, and the encoding is what the physical layer needs to do. It makes it possible for the data link layer to feel that the physical layer is 0 and 1, regardless of how the physical layer is done.
What you have to say here is the encoding of Ethernet, which means 0 and 1:
Manchester Code:

(1) Manchester code: Specify the center of each cycle downward jump (the above figure for example) is 1, jump up into 0.
(2) Differential Manchester code: The specified bit start boundary has a jump of 0, no jump of the representative 1.
In this way, the data becomes a bit stream, to the Novice Web server. Sixth step: server receives bit stream parsing data

After a period of time, the rookie website received a series of bit streams. They don't know what it is.
But they read the content in terms of layering ideas and protocols.
(1) First, it uses the slow Chester code to convert the digital signal into binary information.
(2) and then read each frame according to the ethernetii frame format.
(3) then read each packet in the format of the IP protocol.
(4) The IP packet, after removing the header, becomes the TCP message. If the IP packet is segmented, a TCP message is composed of several IP packets.
(5) The server has already opened 80 ports to wait for the request to come over, this is the HTTP protocol to resolve the content. There are users who need my C + + tutorials. Seventh Step: Server return response

From the user's point of view, the server return response is a C + + tutorial.
However, for a request, the server may need to request classification, operation, generate HTML code files, and then send a HTTP response message to Xiaoming's host.
The process is step-by-step, specific reference to a number of Web server development books to understand.
Let's take a look at the response message:

It includes the HTTP protocol version, status code, Response time, data encoding, and an HTML data.
The server then sent the message to Xiaoming's host. Eighth step: Browser resolution of the Web page file and display the Web page

This again through the layer of encapsulation and decryption, this response message finally to the browser of Xiaoming.
This is how to translate HTML code into a C + + tutorial to become a browser kernel thing.

What the browser kernel does is translate the code on the right side of the picture into the left tutorial.
So how does it work?
First you need to identify HTML elements, which is the title, which is the link, which is the picture.
Second, it constructs a parse tree whose nodes contain various information about the elements.
The browser then calculates the size and height color of each element according to the tree.
Finally, the browser successfully displayed the C + + tutorial.
This is an approximate process. You can learn more about how browsers work by referring to this document. Summary: Large to small, clear division of labor, follow the rules

Finally I finished writing this blog, give me a little praise.
So now we're going to sum up why some computer networks can work methodically in such a complex and huge system.
I summed it up for 12 words above.
Large to small, clear division of labor. The most sensible place for a computer network is to divide computer communication into several levels. Each level is focused only on the part of itself. The lower level provides services to the upper floors.
Follow the rules. Each layer has a few protocols to achieve this level of functionality. So each layer is like communicating with the same layer, which simplifies the flow of the passage greatly. They communicate with the protocol, so that the receiver can use the same protocol to resolve the data of the sending party, and finally realize the network interconnection.

Related Article

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.