tcp/ip, Http, sockets, Xmpp-from Getting started to in-depth

Source: Internet
Author: User
Tags ack http post connection reset

tcp/ip, Http, sockets, Xmpp-from Getting started to in-depthUltimate iOS Program ape 2016-12-29 18:27

In order to facilitate people's understanding and memory, We first introduce these concepts, and then analyze their differences, and then carry out a detailed analysis.

I. INTRODUCTION of TCP/IP

The IP protocol is the network layer, the TCP protocol is the transport layer, the HTTP protocol is the application layer, and the socket is the code encapsulation and application of the TCP/IP Protocol.

TPC/IP primarily addresses how data travels across the network, and HTTP primarily addresses how data is Packaged.

TCP/IP protocol is used to transfer data, Application layer protocol makes the transmitted data meaningful, there are many application layer protocols, such as http, FTP, telnet, etc., You can also define the application layer Protocol.

For example, The Web page uses the HTTP protocol to encapsulate HTTP text information, and then transmits it to the browser using TCP/IP as the Transport layer Protocol.

second, Socket INTRODUCTION

The socket is not a protocol, but an interface (API) that can be called. We pass the socket to use the TCP/IP Protocol.

The socket is not necessarily associated with the TCP/IP Protocol. The socket programming interface is designed to adapt to other network protocols as Well. so, the advent of sockets just 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.

TCP/IP is only a protocol, must be implemented specifically, but also to provide external operating interface, this is the socket programming interface. The socket itself is not a protocol, as stated above, it simply provides an interface for TCP or UDP programming.

Iii. Introduction to TCP Three-time handshake

First handshake: the client sends the packet to the server, waiting for the server to confirm;

Second handshake: the server receives and confirms the Client's packet, and also sends a data bar to reply to the client;

Third Handshake: the client receives the Server's packet, sends a confirmation packet to the server, and completes the three handshake after the packet is Sent.

first, an image of the metaphor : I (the Client) sent a message to you (the server), (this is the first handshake), but I do not know you received no, so you sent a "i have received" the email reply to me (this is the second handshake). I received your e-mail, but you do not know whether I have received your e-mail, so you are not sure whether I know "you have received." So I'm going to send you an e-mail to tell you that I got the email (this is the third handshake).

This makes it possible to confirm that communication between the other is Normal.

The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is Complete.

ideally, Once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the Connection.

When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four-time handshake".

Iv. Steps for Socket network connection

Establishing a socket connection requires at least one pair of sockets, one running on the client, called clientsocket, and the other running on the server side, called Serversocket.

The connection between sockets is divided into three steps: server listening, client request, connection Acknowledgement.

1, Server monitoring: The Server-side real-time monitoring network status, waiting for the status of the connection, waiting for the client connection Request.

2, client request: The client socket to make a connection request, to connect to the target is the Server-side socket. The client's socket must indicate the address and port number of the Server-side socket, and then make a connection request to the Server-side Socket.

3. Connection confirmation: when the server side socket hears the client socket request, establishes a new thread, sends the server end socket the description to the client, once the client confirms this description, both sides formally establishes the Connection. The Server-side sockets continue to listen for connection requests from other Clients.

V. Introduction of HTTP

HTTP (hypertext Transfer Protocol) Hypertext Transfer Protocol is an application protocol based on TCP Protocol.

Each request sent by the client requires a server response, and after the request is completed, the connection is actively released. The process from establishing a connection to closing a connection is called a "one-time connection."

Six. The difference between TCP and UDP

TCP is a link-oriented, although the Network's insecure instability determines how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at a minimum, but also to a large extent to ensure the reliability of the connection;

and UDP before transmitting data does not establish a connection with the other party, the data received is not sent to confirm the signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, UDP is a non-connected, unreliable data transmission Protocol.

therefore, the overhead of UDP is much smaller and the data transfer rate is higher and the real time is Better.

So the use of TCP transmission protocol MSN than the use of UDP QQ transmission file is slow, but can not say that QQ communication is not safe, because the programmer can manually verify the data sent to the udp, such as the sender of each packet number and then by the receiver to verify AH What.

Seven. About XMPP

XMPP (extensible Messaging and Presence Protocol, formerly known as Jabber) is an xml-based open instant Messaging protocol, an Internet standard passed by the Internet Engineering Working Group (IETF).

The XMPP network is server-based, which means that clients do not talk to each other directly.

1. How does XMPP actually run, for example, a chestnut :

The Jabber identifier (JID) is the account used when the user logs in, usually like an e-mail address such as [email protected]; the first part is the user name and the latter part is the XMPP server domain Name.

Suppose Juliet ([email Protected]) wants to talk to Romeo ([email Protected]) and their accounts are on a.com and b.net servers respectively. When Juliet enters the message and presses the Send Button:

Juliet's XMPP client sends her message to the a.com XMPP server.

The a.com XMPP server opens a connection to the b.NET XMPP server.

b.NET XMPP server sends messages to ROMEO. If he is not currently online, then store the message for later Delivery.

Romeo and Juliet the XMPP service is provided by two different companies, and when they subpoena each other, they do not have to have an account with each Other's servers, nor must they be members of the other Company.

2. XMPP Features

XMPP is being contacted by internet users for Google Talk Applications.

XMPP and impp, PRIM, SIP (simple) collectively known as the four major IM protocol mainstream, in this 4 large protocols, XMPP is the most flexible.

Distributed: the architecture of the XMPP network is similar to e-mail; the XMPP core protocol communicates by first creating a stream,xmpp to pass the XML data stream with tcp, without a central master server. Anyone can run their own XMPP server, enabling individuals and organizations to take control of their instant messaging experience.

Good elasticity: XMPP In addition to the use of instant messaging, but also used in network management, content feeds, collaboration tools, file sharing, gaming, remote system monitoring and so On.

Security: the XMPP protocol server can be independent of the public XMPP network (for example, within an intranet), and the reliable security of technologies such as SASL and TLS has been built into the core XMPP technical Specifications.

3. Interconnection with other protocols

Another feature of the XMPP protocol is transport (transports), also known as a gateway (gateways), which allows users to use other protocols over the Network. This can be another instant messaging protocol, or it can be a different protocol, such as a text message (sms) or E-mail.

Inter-communication between each im:

4. The XMPP protocol is shipped via HTTP

The XMPP protocol can use HTTP in two ways: polling (polling) and binding (binding).

Polling is not now recommended, polling: HTTP messages are stored on the Server-side database, and clients must repeatedly crawl (and publish) messages in the form of HTTP GET and Post.

Binding: the client retains a long-lasting http connection, waiting for the message to be received as soon as the server has a new message. Because polling often results in no new messages on the server side, this push notification pattern is more efficient than polling.

5. Another chestnut, Client 1 and Server-side dialog using the XMPP protocol:

Client 1 connects to an XMPP server, sends a message (subject and content of "test 1449") to client 2, and then logs off.

    • Client 1:

<?xml version= "1.0"?>

<stream:stream xmlns:stream= "http://etherx.jabber.org/streams" xmlns= "jabber:client" to= "xmpp server" >

    • XMPP server:

<stream:stream xmlns= ' jabber:client ' xmlns:stream= ' http://etherx.jabber.org/streams ' from= ' XMPP server ' id= ' 1461777714 ' >

    • Client 1:

<iq type= "set" id= "auth_2" to= "xmpp server" >

<query xmlns= "jabber:iq:auth" >

<username> Client 1</username>

<password>mypassword</password>

<resource>Work</resource>

</query>

</iq>

    • XMPP server:

<iq from= "xmpp server" id= ' auth_2 ' type= ' result '/>

    • Client 1:

<message to= "client [email protected]" >

<subject>test 1449</subject>

<body>test 1449</body>

</message>

<presence type= "unavailable" >

<status>logged out</status>

</presence>

</stream:stream>

    • XMPP server:

</stream:stream>

eight, TCP/IP in depth

1. TCP/IP Data format

Easy to see, the following is translated into Chinese after the picture:

The meaning of each field:

    • Source port and Destination port (destination port): 16 bits, respectively, used to distinguish between different processes in the host, and IP addresses are used to distinguish between different hosts, The source port number and destination port number match the source IP address and destination IP address in the IP header to uniquely determine a TCP Connection.

    • Sequence number (data sequence): used to identify the data stream sent from the TCP originator to the TCP receiver, which represents the ordinal of the first data byte in the data flow in the packet segment, and is mainly used to solve the problem of network report Chaos.

    • Acknowledgment number (confirmation ordinal): 32 bits, containing the next sequence number expected to be received at the end of the send acknowledgement, so the confirmation sequence number should be the last time the data byte sequence number plus 1 has been successfully received. however, The confirmation Sequence Number field is valid only if the ACK flag in the flag bit (described Below) is 1 o'clock. Mainly used to solve the problem of not losing packets;

    • Offset: to the number of the first, this value is required because the length of the optional field is Variable. This field accounts for 4bit (up to 15 32bit of words, that is, the first ministerial of 4*15=60 bytes), so TCP has a maximum of 60 byte Headers. however, There is no optional field and the normal length is 20 bytes;

    • There are 6 flag bits in the TCP flags:tcp header, many of which can be set to 1 at the same time, mainly for manipulating Tcp's state machine, in turn, urg,ack,psh,rst,syn,fin. Each flag bit means the following:

URG: This flag indicates that the TCP Packet's emergency pointer field (which is about to be said Immediately) is valid to ensure that the TCP connection is not interrupted and that the Middle-tier device is being processed as soon as possible;

ACK: This flag indicates that the answer domain is valid, that is, the preceding TCP answer number will be included in the TCP packet, there are two values: 0 and 1, The time of 1 indicates that the answer domain is valid, and vice versa is 0;

PSH: This flag bit indicates the push Operation. This means that the packet is delivered to the application immediately after it arrives at the receiving end, rather than queued in the buffer;

RST: This flag indicates a connection reset Request. Used to reset the connection that generated the error and is also used to reject errors and illegal packets;

SYN: indicates the synchronization sequence number used to establish the Connection. The SYN flag and ACK flag bits are used in conjunction with the syn=1,ack=0 when the connection is requested, the syn=1,ack=1 when the connection is being responded to, and the packet of this flag is often used for port Scanning. The scanner sends a packet with only syn, and if the host responds with a packet back, it indicates that the host has this port, but since this scan is only the first handshake of the TCP three handshake, the success of this scan indicates that the machine being scanned is not very secure. A secure host will force a tightly connected three-time handshake to tcp;

Fin: indicates that the sender has reached the end of the data, that is to say, the data transfer is complete, No data can be transferred, send the FIN flag bit TCP packets, The connection will be Disconnected. The packet of this flag is also often used for port Scanning.

    • Windows: window size, known as a sliding window, for flow control; This is a complex problem.

2, TCP/IP Three-time Handshake

The TCP protocol is able to provide a reliable connection because of a three-time handshake. The purpose of the Three-time handshake is to synchronize the serial number and confirmation number of both parties and Exchange TCP window size Information.

The SEQ in the first brief says: Sequence number (data ordinal). SYN: indicates the synchronization sequence Number. ACK: is not a flag in TCP flags, but refers to acknowledgment number (confirmation ordinal).

First handshake: the client sends the connection request message segment, The SYN position is 1,sequence number (data Ordinal) to x, and then the client enters the Syn_send state, waiting for the server to confirm;

Second handshake: the server receives the Client's SYN message segment, confirms the SYN segment, sets acknowledgment number (confirmation Sequence) to x+1 (Sequence number+1), and sends the SYN request information itself. The SYN position is 1,sequence number y, and the server puts all of the above information into a message segment (that is, the Syn+ack message segment) and sends it to the client, where the server enters the SYN_RECV state;

Third Handshake: the client receives the Server's Syn+ack message Segment. Then the acknowledgment number is set to y+1, send an ACK message to the server segment, after the message segment is sent, the client and server side both enter the established state, complete the TCP three Handshake.

So if the Three-time handshake is changed to two-time handshake then what is the problem: the failed connection request message segment is suddenly transmitted to the server, resulting in an error. Detailed

    • The first connection request message segment sent by the client is not lost, but it is stuck in a network node for a long time, causing it to delay until the connection is released before it reaches the Server. Originally this is a message segment that has already expired. however, after the server receives this failed connection request message segment, It is mistaken for a new connection request from the Client. The client is then sent a confirmation message segment, agreeing to establish a Connection. Assuming that the "three-time handshake" is not used, The new connection is established as soon as the server issues a Confirmation.

    • Because the client is now not making a connection request, the server acknowledgement is ignored and data is not sent to the Server. But the server thought the new transport connection had been established and waited for the client to send the Data. In this way, many of the Server's resources are Wasted. The use of "three-time handshake" method can prevent the above Phenomenon. For example, in that case, the client does not issue confirmation to the Server's Confirmation. The server knows that the client does not require a connection because it cannot receive a Confirmation. ”

4, three times after the handshake can be transmitted Data. After the data is transferred, the TCP connection is disconnected, and this is the fourth time that TCP breaks up.

First break Up: Host 1 (can be client or server side), set sequence number and acknowledgment number, send a fin message segment to host 2, at which point, Host 1 enters fin_wait_1 state This indicates that the host 1 has no data to be sent to host 2;

Second Breakup: Host 2 received the Host 1 sent fin message segment, to the host 1 back an ACK message segment, acknowledgment number is sequence number plus 1; Host 1 enters Fin_wait_2 state; host 2 tells host 1, I "agree" Your request for closure;

Third Breakup: host 2 to the host 1 send fin message segment, request to close the connection, while the host 2 into the Last_ack state;

Fourth Break: Host 1 received the Host 2 sends the FIN message segment, sends the ACK message segment to the host 2, then the host 1 enters the time_wait state, the host 2 receives the host 1 ACK message segment, closes the connection, at this time, the host 1 waits for 2MSL to still not receive the reply, This proves that the server side has shut down properly, so the host 1 can also shut down the Connection.

5, divide a hand also want four times, bored not to bother, see why break up to so trouble:

TCP protocol is a connection-oriented, reliable, byte-stream-based Transport Layer Communication Protocol.

TCP is a Full-duplex mode, which means that when the host 1 is issued Fin segment, only indicates that the host 1 has no data to send, Host 1 tells the host 2, its data is all sent out, but this time the host 1 can still accept data from host 2, when Host 2 returns an ACK segment, Indicates that it already knows that the host 1 no data sent, but host 2 can still send data to host 1, and then host 2 also sent Fin segment, this time indicates that the host 2 also no data to send, will tell the host 1, I also have no data to send, and then interrupted the TCP Connection.

6, four times the state changes during the BREAKUP.

Fin_wait_1: in fact, the real meaning of fin_wait_1 and fin_wait_2 states is to wait for each other's fin message. The difference between the two states is: the fin_wait_1 state is actually when the socket in the established state, it would like to actively close the connection, send a FIN message to the other side, when the socket is entered into the fin_wait_1 State. And when the other party responds to the ACK message, then into the fin_wait_2 state, of course, under the actual normal circumstances, regardless of the circumstances of each other, should immediately respond to the ACK message, so fin_wait_1 state is generally more difficult to see, and Fin_wait_ 2 states can also sometimes be seen with netstat. (active Side)

Fin_wait_2: the above has explained in detail this state, in fact, the fin_wait_2 state of the socket, that is, the semi-connection, that is, one side requires close connection, but also tell the other side, I temporarily have a bit of data to send you (ack information), and then close the Connection. (active Side)

Close_wait: indicates waiting to be closed. When the other side close a socket to send fin message to yourself, you will undoubtedly respond to an ACK message to each other, then enter into the close_wait State. next, The real thing you really need to consider is whether you still have the data sent to the other person, if not, then you can close the socket, send fin messages to each other, that is, close the Connection. So what you need to accomplish in the close_wait state is waiting for you to close the Connection. (passive Side)

Last_ack: It is a passive shutdown after sending a fin message and finally waiting for the other Party's ACK Message. When an ACK message is received, It is also possible to enter the closed available State. (passive Side)

Time_wait: said to receive the other side of the fin message, and sent out an ACK message, just wait for 2MSL to return to the closed usable State. If the FINWAIT1 state, received the other side with the FIN flag and the ACK flag message, you can directly into the time_wait state, without having to go through the fin_wait_2 State. (active Side)

CLOSED: indicates a connection Interruption.

nine, http Detailed

1, the HTTP protocol is always the client initiated the request, the server loopback response. A client makes a request to the server and then the server returns a response (response), and the connection is Closed.

2, has done the socket programming people all know, the message Head/message body "the Division Way is very commonly used, the message head tells the other party this message is what, the message body tells the other party how to do." Each HTTP packet is divided into HTTP header and HTTP body two parts, the message body is optional, and the message header is Necessary. Whenever we open a webpage,

The HTTP request consists of three parts: the request line, the request header, and the request Body. The request line refers to the request method URI protocol/version,

The format is as Follows: post/index.php http/1.1 is the version of the Request/url Protocol/protocol.

3. Request method

Http/2: This version is officially released in May 2015. HTTP/2 reduces latency by supporting requests and corresponding multiplexing, minimizing protocol overhead by compressing HTTP header fields, and increasing support for request prioritization and Server-side Push.

The http/1.1 protocol defines 8 methods of HTTP Requests.

    • The Get:get request displays the resource specified by the Request. generally, The Get method should be used only for reading data, not for non-idempotent operations that produce side Effects.

The Get method requests the specified page information and returns the response body, and get is considered an unsafe method because the Get method is accessed arbitrarily by the network Spider.

    • The Head:head method, like the Get method, is a request to the server to make a specified resource. however, the server does not return the content part of the resource in response to the head request, that is, the response body. In this way, we can get the Server's response header information without transmitting the entire Content. The head method is often used by clients to view the performance of the Server.

    • The Post:post request submits data to the specified resource and requests the server to process it, such as: form data submission, file upload, etc., and the request data is included in the request Body. The Post method is a non-idempotent method, because this request may create new resources Or/and Modify existing Resources.

    • The put:put request will upload its latest content to the specified resource location, and the Put method is a idempotent method. This method allows the client to transfer the most recent data from the specified resource to the server in place of the specified Resource's Content.

    • The Delete:delete request is used to request the server to delete the resource identified by the requested URI (uniform resource identifier, Uniform Resource Identifier). Deletes the specified resource after the delete request, and the Delete method is Idempotent.

    • The Connect:connect method is reserved by the http/1.1 protocol and is able to change the connection to a proxy server for pipeline Mode. A link to an ssl-encrypted server is typically used to communicate with a non-encrypted HTTP proxy server.

    • Options:options requests are similar to head and are typically used for client View server Performance. This method requests the server to return all the HTTP request methods supported by the resource, which uses ' * ' instead of the resource name to send the options request to the server to test whether the server is functioning PROPERLY. When a JavaScript XMLHttpRequest object is shared with a cors Cross-domain resource, The options method is used to send a sniffer request to determine whether there is access to the specified resource. Allow

    • Trace:trace requests the server to echo its received request information, which is used primarily for testing or diagnosing HTTP Requests.

4, after the http/1.1 standard has been formulated, and gradually expanded a number of METHODS. The patch method is more commonly used:

    • The Patch:patch method is defined in the RFC 5789 standard for 2010. Patch requests are similar to put requests and are also used for updates to Resources. The following two points are different:

Patches are typically used for partial updates of resources, while put is generally used for overall resource updates.

When a resource does not exist, patch creates a new resource, and put only updates the Resource.

5, idempotent (idempotence)

The idempotent nature of an HTTP method means that one and more requests for a resource should have the same side effects. Idempotent is a semantic category, just as the compiler can only help check for syntax errors, and the HTTP specification does not have the means to define it by means of syntax such as message format, which may be one of the reasons why it is less valued. But in fact, idempotent is a very important concept in the design of distributed system, and the distributed nature of HTTP also determines its important position in HTTP.

The HTTP protocol itself is a resource-oriented application layer protocol, But there are actually two different ways to use the HTTP protocol: one is restful, it treats HTTP as an application layer protocol, and more faithfully adheres to the various provisions of the HTTP protocol; the other is soa, Instead of using HTTP as an application-level protocol, it takes the HTTP protocol as the Transport layer protocol, and then builds its own Application-layer protocol over HTTP. The power of HTTP is mainly for RESTful style, idempotent is not a specific protocol, it is a characteristic of distributed system, so both SOA and RESTful Web API design should consider Idempotent. The semantics and idempotent of HTTP GET, DELETE, PUT, post four main methods are described BELOW.

The HTTP get method is used to obtain resources and should not have side effects, so it is Idempotent.

The HTTP Delete method is used to delete resources with side effects, but it should satisfy Idempotent. For example: Delete http://www.xxxxx.com/article/4231, call once and n times to the system side effect is the same, that is, the deletion of the post ID 4231; therefore, The caller can call or refresh the page multiple times without worrying about causing an error.

It is easy to confuse HTTP post and Put. The difference between post and put is easy to think of as "post means creating resources, put means updating resources"; in fact, Both can be used to create resources, the more essential difference is idempotent. This is defined in the HTTP specification for post and put:

The URI corresponding to the post is not the resource itself, but the recipient of the Resource. For example, using post to submit a post, two times the same POST request will create two resources on the server side, they have different uris, so the Post method is not Idempotent. The URI for the put is the resource itself to be created or updated. For example, PUT creates or updates a post with an ID of 4231. The side effects of multiple put on the same URI are the same as a put, so the Put method is Idempotent.

For example, The Forum website prevents accidental duplication of posts:

Use post to implement post, and put to implement update Posts.

This article is for the headline author and does not represent Today's headline Position.

    • Programmers
    • Communication
    • Xml
    • Technology
Favorite Reports

-Article comments<textarea name="inputText"></textarea>Comments
  • eating, drinking, gambling, teenagers, 31 minutes ago .

    Good writing, support!

    reply 1
  • Write a diary 2 hours ago

    Quite well, is learning this knowledge. Thank you

    reply 1
  • Zuoxue88 3 hours ago

    It's really good to support the Author.

    reply 0
  • Ultimate iOS Program ape 3 hours ago

    We collect a Lot. Thank you

    reply 7
  • Jing Wei Tao Sound 98084146 1 hours ago

    English code too many, who can remember, that level are invisible things, communication system things to accumulate experience from the foundation, especially these codes are english, it is easy to confuse,

    Reply

tcp/ip, Http, sockets, Xmpp-from Getting started to in-depth

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.