The difference between TCP/IP and HTTP

Source: Internet
Author: User
Tags file transfer protocol

TPC/IP Protocol is a Transport layer protocol, which mainly solves how data is transmitted in the network, and HTTP is the application layer protocol, which mainly solves how to wrap the data. regarding the relationship between TCP/IP and HTTP protocol, the network has a relatively easy to understand: "When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but in that case, if there is no application layer, it will not be able to identify the data content, if you want to make the transmitted data meaningful, You must use the Application layer protocol, there are many application layer protocols, such as HTTP, FTP, Telnet, etc., you can also define the application layer protocol. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. ”

The term TCP/IP stands for Transmission Control Protocol/Internet Protocol, which refers to a series of protocols. "IP" represents an Internet protocol in which TCP and UDP use the protocol to transfer packets from one network to another network. Put IP imagined as a highway, it allows other protocols to travel on it and find the exits of other computers. TCP and UDP are "trucks" on highways, and the goods they carry are like HTTP, File Transfer Protocol FTP and so on. You should be able to understand that TCP and UDP are the transport layer protocols used by FTP,HTTP and SMTP. Although both TCP and UDP are used to transmit other protocols, they have a significant difference: TCP provides guaranteed data transfer, and UDP does not.    This means that TCP has a special mechanism to ensure that data is safe from one endpoint to another without error, and UDP does not provide any such assurance. HTTP (Hypertext Transfer Protocol) is a protocol that leverages TCP to transfer information between two computers, typically Web servers and clients.  The client initiates an HTTP request to the Web server using a Web browser, and the Web server sends the requested information to the client. The HTTP protocol is based on the TCP protocol, which sends an HTTP request when the browser needs to fetch the Web page data from the server. HTTP will establish a connection channel to the server via TCP, and when the data required for this request is complete, HTTP will immediately disconnect the TCP connection, which is a very short process. So an HTTP connection is a short connection, a stateless connection.the so-called stateless, refers to the browser every time the request to the server, not through a connection, but each time to establish a new connection. If it is a connection, the server process can hold the connection and remember some information status in memory. And each time the request ends, the connection is closed, the related content is released, so you can not remember any state to become a stateless connection. from http/1.1 onwards, the default is to open the keep-alive, to maintain the connection characteristics, in short, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection will not be closed, if the client again access to the Web page on this server, will continue to use this established connection keep-alive does not permanently maintain the connection, it has a hold time and can be set in different server software (such as Apache) this time. Although the TCP connection is maintained here for a period of time, the time is limited and the time is still closed, so we consider it to be closed each time the connection is completed. Later, through the session, cookies and other related technologies, can also maintain some user status. However, each time a connection is used, the connection remains stateless.   HTTP is a short Connection when the TCP connection is turned off after each request is completed. When we use the TCP protocol directly through socket programming, we can control when the connection is closed by the code area, as long as we do not close the connection through the code, the connection will persist in the process of the client and the server. The relevant state data is kept.

In C # There will be sockets, in fact the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a calling interface (API). The advent of sockets only makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as Create, listen, connect, accept, send, Read and write, and so on.

Description of the comparison image: HTTP is a sedan that provides a specific form of encapsulation or display data; The socket is the engine that provides the ability to communicate over the network. In terms of programming from C #, for convenience, you can directly choose which car HTTP has been made to interact with the server. But sometimes, because of environmental factors or some other custom requests, you have to use the TCP protocol, then you need to use socket programming, and then handle the data you get. It's like you're building a truck with your existing engine and going to interact with the server.


Both http/1.0 and http/1.1 use TCP as the underlying transport protocol. The HTTP client initiates the establishment of a TCP connection with the server first. Once the connection is established, the browser process and the server process can access TCP through their sockets. As mentioned earlier, the client socket is the "gate" between the client process and the TCP connection, and the server-side socket is the "gate" between the server process and the same TCP connection.

The following diagram attempts to show the location of the different TCP/IP and other protocols in the original OSI model:

7 Application Layer such as HTTP, SMTP, SNMP, FTP, Telnet, SIP, SSH, NFS, RTSP, XMPP, Whois, ENRP
6 Presentation Layer such as XDR, ASN.1, SMB, AFP, NCP
5 Session Layer For example ASAP, TLS, SSH, ISO 8327/ccitt x.225, RPC, NetBIOS, ASP, Winsock, BSD sockets
4 Transport Layer such as TCP, UDP, RTP, SCTP, SPX, ATP, IL
3 Network layer such as IP, ICMP, IGMP, IPX, BGP, OSPF, RIP, IGRP, EIGRP, ARP, RARP, x.
2 Data Link Layer such as Ethernet, Token Ring, HDLC, Frame Relay, ISDN, ATM, IEEE 802.11, FDDI, PPP
1 Physical Layer such as line, radio, optical fiber, homing pigeon

1. Several important concepts of HTTP protocol
1. Connection (Connection): The actual circulation of a transport layer, which is built between two applications that communicate with one another.
2. Message: The basic unit of HTTP communication, including a structured eight-tuple sequence and transmission via a connection.
3. Request: A request from the client to the server includes the method that is applied to the resource, the identifier of the resource, and the version number of the Protocol
4. Response (Response): A message returned from the server includes the version number of the HTTP protocol, the status of the request (for example, "succeeded" or "not Found"), and the MIME type of the document.
5. Resource (Resource): A network data object or service that is identified by a URI.
6. Entity: A special representation of a data resource or a reflection from a service resource, which may be surrounded by a request or response message. An entity includes entity header information and the entity's own content.
7. Client: An application that establishes a connection for the purpose of sending a request.
8. User Agent (useragent): Initializes a requested client. They are browsers, editors, or other user tools.
9. Server: An application that accepts a connection and returns information to the request.
10. Source Server (Originserver): is a server on which a given resource can reside or be created.
11. Proxy: An intermediary program that can act as a server or as a client to establish requests for other clients. Requests are either internally or passed to other servers through possible translations. An agent must interpret and overwrite it if possible before sending the request information.
Proxies are often used as portals through the firewall's client side, and proxies can be used as a help app to handle requests that are not completed by the user agent through the protocol.
12. Gateway: A server that acts as an intermediary for other servers. Unlike the proxy, the gateway accepts the request as if it were the source server for the requested resource, and the requesting client is unaware that it is dealing with the gateway.
Gateways are often used as server-side portals through firewalls, and gateways can be used as a protocol translator to access resources stored in non-HTTP systems.
13. Channel (tunnel): is a broker that acts as a two connection relay. Once activated, the channel is considered not to be an HTTP communication, although the channel may be initialized by an HTTP request. The channel disappears when both ends of the relayed connection are closed. A channel is often used when a portal must exist or the intermediary (intermediary) cannot interpret the relay's traffic.
14. Cache: Local storage of the reaction information.

2. Sending the request
After a connection is opened, the client sends the request message to the server's dwell port to complete the request action.
The format of the http/1.0 request message is:
Request message = Request Line (General Information | request Header | entity header) crlf[entity content]
Request line = Method Request URL http version number CRLF
Method =get| head| Post| extension methods
U R l= protocol name + host name + directory and file name
The methods in the request line describe the actions that should be performed in the specified resource, and the commonly used methods are, and post. The result of a different request object corresponding to get is different, and the corresponding relationship is as follows:
Result of Object get
Contents of the File file
Program execution results of the program
Database query Query Results
HEAD?? Requires the server to look up the meta information of an object, not the object itself.
POST?? Data is transferred from the client to the server, and the Post method is used when the server and CGI are required to do further processing. Post is primarily used to send the contents of a form in HTML text to be processed by the CGI program.
An example of a request is:
gethttp://networking.zju.edu.cn/zju/index.htmhttp/1.0 networking.zju.edu.cn/zju/index.htmhttp/1.0 header information is also called meta-information, Information, the use of meta-information can be used to achieve conditional requests or responses.
Request header?? Tell the server how to interpret the request, mainly including the type of data that the user can accept, the compression method, and the language.
Solid head?? Entity information type, length, compression method, last modification time, data validity period, and so on.
Entity?? Requests or answers the object itself.
3. Send a response
The server sends a response message to the client after it has finished processing the client's request.
The response message format for http/1.0 is as follows:
Response message = Status line (General Information Header | response header | entity header) crlf(entity content)
Status line =http version number status code reason narration
Status codes represent response types
1XX reserved
2XX indicates that the request was successfully received by
3XX request further refinement of requests for completion of customer requests
4XX Customer Error
5XX Server Error
The response header information includes the service program name, notifies the client that the requested URL requires authentication, and when the requested resource is available.
4. Close the connection
Both client and server can end TCP/IP conversations by closing sockets

Source: http://www.cnblogs.com/renyuan/archive/2013/01/19/2867720.html

The difference between TCP/IP and HTTP

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.