As a Java EE developer, the most common possibility is the HTTP protocol, but we are really clear about the HTTP protocol and the layers of protocols underneath it. Now we're going to get a rough idea.
First, Application layer-http
Second, Transport layer-TCP
Third, the network layer-IP One, application layer-http
An HTTP request consists of three components:
1. Method-Uniform Resource Identifier (URI)-Protocol/version
2. The requested head
3. Main content
The following is an example of an HTTP request:
post/test/index.jsp http/1.1
accept:text/plain, text/html
accept-language:zh-cn,zh;q=0.8,en;q=0.6
Connection:keep-alive
host:localhost
user-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
content-length:27
content-type:application/x-www-form-urlencoded
Accept-encoding:gzip, deflate
username=test&passwd=123456
Method-Uniform Resource Identifier (URI)-Protocol/version, appearing on the first line.
post/test/index.jsp http/1.1
This is the Post request method,/test/index.jsp is the URI. The http/1.1 is the Protocol/version section.
Each HTTP request can use one of several methods in the HTTP standard. HTTP 1.1 supports the type of request in 7: Get,post,head,options,put,delete and Trace. Get and post are most commonly used on the Internet.
The URI completely indicates an Internet resource, and the URI is usually interpreted as the root directory of the server. Therefore, always start with/.
The requested header contains useful information about the client environment and the subject content of the request.
Accept:text/plain, text/html an acceptable data type.
Accept-language the type of language that can be received.
Connection:keep-alive This connection remains a certain amount of time, when the client sends another request, it will use the same connection, in response, the server in response to the secondary connection will also add a connection:keep-alive in the response header, The specific length of the connection is implemented differently depending on the server.
Host:localhost the server address of the connection.
user-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) Current client environment information.
The length of the requested body content that the content-length:27 sends, i.e. the length of the username=test&passwd=123456 content.
Content-type:application/x-www-form-urlencoded the requested body content type sent.
Accept-encoding:gzip, deflate the type of data that can be received.
Finally, the request body's data content is username=test&passwd=123456. Note that there is a newline between the request header and the request body, which is the key to distinguishing between the two parts.
The above is the HTTP protocol request data, as a Web server (tomcat), in fact, is a TCP server, when the socket request is received, it will get the secondary socket connection InputStream input stream object to the Request object, Gets the OutputStream output stream object for this secondary socket connection to the response object. The input stream object can read all of the information sent above, and then encapsulate it into the corresponding properties of the request object, such as Requestmap,requestnames,requesturi,cookie properties, We can obtain the client request information through the appropriate getter method. Gets the requested Servlet object instance based on the requested URI reflection, and then passes the encapsulated request object and the response object to the Servlet's service method and executes the logic within the service. Finally, the data is written to the client via the response object (that is, the output stream object of the secondary socket connection). An HTTP request is over.
The above description is only part of the HTTP protocol. Next we'll say the following HTTP protocol. second, transport layer-TCP
The Application layer HTTP protocol is based on the Transport Layer TCP protocol. TCP protocol is a reliable transmission, FULL-DUPLEX protocol, reliable transmission refers to the protocol on the unreliable IP protocol to do their own acceptance and time-out mechanism, so the connection required three times before the handshake, and established a long link. Full-duplex refers to the sender and receiver can send data, that is, two-way communication, so disconnect before you need to wave four times (in some cases there is a half connection, that is, a party sent Syn to tell the other party itself no longer send data, but also can receive data from the other side). A TCP datagram contains the requester application port number, the responder application port number, that specifies the application that needs to be connected.
The reliability of TCP protocol gives us a powerful data transmission guarantee, we do not need to care about the existence of packet loss. Full-duplex Communication (bidirectional communication) is possible because the application port number of the requester and the corresponding party exists in the TCP datagram. Bi-directional communication for our HTTP connection:keep-alive to do the protection, keep-alive to maintain the connection can be a number of two-way transmission of data, can save the establishment of multiple connections, and http1.1 protocol exists in the half of the connection is also based on this.
IP is a stateless protocol, that is, each IP datagram sent routing is changed, which led to the receiver received the datagram is unordered, but the TCP protocol in the header there is a sort of datagram, so through the TCP protocol finally received data will be sorted, So that we can see a complete and orderly data. Third, the network layer-IP
After that, TCP, we'll talk about the IP protocol next. The TCP protocol calls the IP protocol, and the IP protocol is responsible for addressing only. We all know that the first step in requesting a domain name is to go to DNS to resolve the domain name to obtain the IP address behind the domain name, because all protocols below are the IP addresses used (except Domain name Resolution Protocol). If we request a domain name to a certain number of times, our computer will be the IP cache behind the domain in memory, the next time it will no longer resolve the domain name, which saves the domain name resolution of the time spent. Of course, the cached IP will be emptied when we shut down the computer.
An IP address we can interpret it as three parts, according to the different subnet mask, we can find the corresponding network number, subnet number, host number. Look for the corresponding subnet number through the large network number, and then find the target host number through the subnet number.
IP routing addressing mainly completes the following functions:
1. Search the routing table to find the table (network number and host number) that match the destination IP address exactly. If found, the message is sent to the next station router or directly connected network interface specified in the table.
2. Search the routing table to find the table that can match the destination network number. If found, the message is sent to the next station router or directly connected network interface specified in the table.
3. Search the routing table for the table labeled "Default". If found, the message is sent to the next station router specified in the table.
4. If none of the above steps succeed, the datagram cannot be transmitted. If a datagram that cannot be delivered comes from this computer, it is generally possible to return a "host unreachable" or "Network unreachable" error to a useful program that generates datagrams.
The above is an overview of network-related content.
My blog: https://qbian61.github.io/