TCP, UDP, HTTP, SOCKET

Source: Internet
Author: User
Tags time and date

TCP HTTP UDP:

is the communication protocol, that is, the rules of communication, only the two parties in accordance with this rule "speak", the other party can understand or serve.

TCP/IP stands for Transmission Control Protocol/Internet Protocol, which refers to a series of protocols.

   TCP和UDP使用IP协议从一个网络传送数据包到另一个网络。把IP想像成一种高速公路,它允许其它协议在上面行驶并找到到其它电脑的出口。TCP和UDP是高速公路上的“卡车”,它们携带的货物就是像HTTP,文件传输协议FTP这样的协议等。   TCP和UDP是FTP,HTTP和SMTP之类使用的传输层协议。虽然TCP和UDP都是用来传输其他协议的,它们却有一个显著的不同:TCP提供有保证的数据传输,而UDP不提供。这意味着TCP有一个特殊的机制来确保数据安全的不出错的从一个端点传到另一个端点,而UDP不提供任何这样的保证。    HTTP(超文本传输协议)是利用TCP在两台电脑(通常是Web服务器和客户端)之间传输信息的协议。客户端使用Web浏览器发起HTTP请求给Web服务器,Web服务器发送被请求的信息给客户端。   记住,需要IP协议来连接网络;TCP是一种允许我们安全传输数据的机制,,使用TCP协议来传输数据的HTTP是Web服务器和客户端使用的特殊协议。   Socket 接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,用以开发TCP/IP网络上的应用程序。
TCP HTTP UDP Three relationships: TCP/IP is a protocol group

Can be divided into four levels: network interface layer, network layer, Transport layer and application layer.

At the network layer are IP protocols, ICMP protocols, ARP protocols, RARP protocols, and BOOTP protocols.

There are TCP protocols and UDP protocols in the transport layer.

In the application layer, there are FTP, HTTP, TELNET, SMTP, DNS and other protocols.

http

Therefore, HTTP itself is a protocol that transmits hypertext to a local browser from a Web server.

Socket

This is to achieve the above communication process to establish a communication pipeline, its real representative is a client and server side of a communication process, the two sides of the process through the socket to communicate, and the rules of communication using the specified protocol. Socket is just a connection mode, not protocol, TCP, UDP, simple (although inaccurate) is the two most basic protocol, many other protocols are based on these two protocols, such as HTTP is based on TCP,. You can create a TCP connection with a socket, or you can create a UDP connection. This means that a socket can be used to create a connection to any protocol, because other protocols are based on this.

Here's a look at the protocols that are closely related to our Internet life: HTTP

What is the HTTP protocol

HTTP full name is Hypertext Transfer Protocal, that is: Hypertext Transfer Protocol, from 1990 onwards in the WWW widely used, is now the most widely used in the WWW protocol, HTTP is the application layer protocol, when you surf the Web page, Data is sent and received over the Internet through HTTP between the browser and the Web server. HTTP is a stateless protocol based on the request/response pattern. That's what we usually call request/response.

Url:

The URL (Uniform Resource Locator) address is used to describe a resource on a network with the following basic format

schema://host[:port#]/path/…/[?query-string][#anchor]scheme               指定低层使用的协议(例如:http, https, ftp)host                   HTTP服务器的IP地址或者域名port#                 HTTP服务器的默认端口是80,这种情况下端口号可以省略。如果使用了别的端口,必须指明,例如 http://www.cnblogs.com:8080/path                   访问资源的路径query-string       发送给http服务器的数据anchor-             锚

An example of a URL

http://www.mywebsite.com/sj/test/test.aspx?name=sviergn&x=true#stuff

Schema:http
Host:www.mywebsite.com
Path:/sj/test/test.aspx
Query String:name=sviergn&x=true
Anchor:stuff

HTTP Request/response:

Look at the structure of the request message, the request message is divided into 3 parts

The first part is called Request line,

The second part is called the request header,

The third part is body. There is a blank line between the header and the body,

Structure such as

The method in the first line represents the request methods, such as "POST", "GET", Path-to-resoure represents the requested resource, and Http/version-number represents the version number of the Http protocol

When the "GET" method is used, the body is empty

For example, we open the Blog Garden home page request as follows

GET http://www.cnblogs.com/ HTTP/1.1Host: www.cnblogs.com

Abstract things, difficult to understand, the old feeling is virtual, the so-called seeing is real, actually see things, we can understand and remember. Today we use Fiddler, actually look at the request and response.

Below we open fiddler capture a blog Park login request and then analyze its structure, in the Inspectors tab under the raw way to see the complete request message,

Such as

Accept

Role: The type of media that can be accepted by the browser side,

For example: accept:text/html represents the type of server postback that the browser can accept as text/html, which is what we often call HTML documents,

If the server cannot return data of type text/html, the server should return a 406 error (non acceptable)

Wildcard * represents any type

For example, Accept: / on behalf of the browser can handle all types, (General browser sent to the server is this)

Referer:

Role: The server that provides the context information for the request tells the server which link I have received from, such as linking to a friend from my home page, and his server is able to count the number of users who clicked the link on my page every day from the HTTP referer to visit his website.

For example:Referer:http://translate.google.cn/?hl=zh-cn&tab=wT

Accept-language

Role: The browser affirms the language it receives.

Language and Character set differences: Chinese is a language, Chinese has a variety of character sets, such as BIG5,GB2312,GBK and so on;

Example: accept-language:en-us

Content-type

Role:

For example:Content-Type: application/x-www-form-urlencoded

Accept-encoding:

Function: The browser declares itself to receive the encoding method, usually specifies the compression method, whether compression is supported, what compression method is supported (Gzip,deflate), (note: This is not a character encoding);

For example:Accept-Encoding: gzip, deflate

User-agent

Role: tells the HTTP server which client uses the name and version of the operating system and browser.

When we go online to the forum, often see some welcome information, which lists the name and version of your operating system, the name and version of the browser you are using, which often makes a lot of people feel very magical, in fact, The server application obtains this information from the User-agent request header domain user-agent The request header domain allows the client to tell the server about its operating system, browser, and other properties.

For example:User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; InfoPath.2; .NET4.0E)

Connection

Example: connection:keep-alive when a Web page opens, the TCP connection between the client and the server for transmitting HTTP data does not close, and if the client accesses the Web page on the server again, it will continue to use the established connection

For example: Connection:close represents the completion of a request, the TCP connection between the client and the server for transmitting HTTP data is turned off, and the TCP connection needs to be re-established when the client sends the request again.

Content-length

Role: The length of the data sent to the HTTP server.

For example:Content-Length: 38

Host (the header field is required when the request is sent)

Role: The request header domain is used primarily to specify the Internet host and port number of the requested resource, which is typically extracted from the HTTP URL

For example: We enter in the browser:http://www.guet.edu.cn/index.html

In the request message sent by the browser, the host Request header field is included, as follows:

Host:http://www.guet.edu.cn

The default port number 80 is used here, and if a port number is specified, it becomes: Host: Specify port number

Pragma

Role: Prevent the page from being cached, in the http/1.1 version, it is identical to the Cache-control:no-cache function

Pargma has only one usage, for example:Pragma: no-cache

Cookies:

Role: The most important header, the value of the cookie is sent to the HTTP server

Accept-charset

Role: The browser affirms its own received character set, this is the various character sets and character encodings described earlier in this article, such as gb2312,utf-8 (usually we say CharSet includes the corresponding character encoding scheme);

The structure of the response message,

The structure of the request message is basically the same. Also divided into three parts

The first part is called Response line,

The second part is called response header,

The third part is body. There is also a blank line between the header and the body.

Structure such as

Http/version-number

For the version number of the HTTP protocol, Status-code and message, see the detailed explanation of the next section [Status code].

We use Fiddler to capture a blog home response then analyze its structure, in the Inspectors tab under the raw way can see the full response message, such as

Cache-control

Role: This is a very important rule. This is used to specify the caching mechanism that response-request follows. Each instruction has the following meanings

Cache-Control:Public   可以被任何缓存所缓存()Cache-Control:Private     内容只缓存到私有缓存中Cache-Control:no-cache  所有内容都不会被缓存

There are other uses, I do not understand the meaning, please refer to other information

Content-type

Role: The Web server tells the browser the type and character set of the object it responds to.

For example:

content-type:text/html; Charset=utf-8

content-type:text/html;charset=gb2312

Content-type:image/jpeg

Expires

Role: The browser will use the local cache for the specified expiration period

For example:

ExpiresTue, 08 Feb 2022 11:35:14 GMTLast-Modified:

Role: Used to indicate the last modification date and time of the resource. (See examples of if-modified-since in the section)

Example: last-modified:wed, Dec 09:09:10 GMT

Server:

Function: Indicates the software information of the HTTP server

For example:

ServerMicrosoft-IIS/7.5X-AspNet-Version:

Role: If the Web site is developed with ASP, this header is used to represent the version of ASP.

For example:

X-AspNet-Version4.0.30319
X-powered-by:

Role: Indicates what technology the site is developed with

For example:X-Powered-By: ASP.NET

Connection

Example: connection:keep-alive when a Web page opens, the TCP connection between the client and the server for transmitting HTTP data does not close, and if the client accesses the Web page on the server again, it will continue to use the established connection

For example: Connection:close represents the completion of a request, the TCP connection between the client and the server for transmitting HTTP data is turned off, and the TCP connection needs to be re-established when the client sends the request again.

Content-length

Indicates the length of the entity body, expressed as a decimal number stored in bytes. In the process of data downlink, content-length the way to pre-cache all the data in the server, and then all the data peremptorily to the client.

For example:Content-Length: 19847

Date

Role: The exact time and date of the message generation

For example:

DateSatFeb 2012 11:35:14GMT
Get and post of HTTP protocol

The HTTP protocol defines a number of ways to interact with the server, the most basic of which are 4, get,post,put,delete, respectively. A URL address is used to describe a resource on a network, and the Get, POST, PUT, delete in HTTP corresponds to the search for this resource, change, increase, delete 4 operations. Our most common is get and post. Get is typically used to get/query resource information, and post is typically used to update resource information.

The difference between get and post
    1. Get submitted data is placed after the URL, to split the URL and transfer data, the parameters are connected with &, such as editposts.aspx?name=test1&id=123456. The Post method is to put the submitted data in the body of the HTTP packet.

    2. The data size for get commits is limited (because the browser has a limit on the length of the URL), and there is no limit to the data submitted by the Post method.

    3. The Get method needs to use Request.QueryString to get the value of the variable, and post by Request.Form to get the value of the variable, that is, get is passed the value through the Address bar, and post is the value of the submission table forms.

    4. The Get method submits the data, which brings security problems, such as a login page, when the data is submitted via get, the user name and password will appear on the URL, and if the page can be cached or someone else can access the machine, the user's account and password can be obtained from the history record.

Here is my own open a public number [wink], in addition to writing technical articles, there are products, industry and life thinking, hope to and more walk on this road peers exchange, interested can pay attention to, thank you.

TCP, UDP, HTTP, SOCKET

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.