IOS Client and WebSocket Communications (i)

Source: Internet
Author: User
Tags ack



My address: http://blog.csdn.net/jinglijun/article/details/9365879

This article we first understand the basic knowledge, this is more helpful to our study behind.

Socket,websocket,http,tcp and so on we have heard the ears have a cocoon, but use the time to review it.

When the university studied the Network foundation, the teacher said that the network is divided into physical layer, data link layer, network layer, transport layer, Session layer, presentation layer and application layer. Through preliminary understanding, I know IP protocol corresponding to the network layer, TCP protocol corresponding to the transport layer, and HTTP protocol corresponding to the application layer, the three are essentially not comparable, the socket is the TCP/IP protocol encapsulation and application (programmer level). It can also be said that the TPC/IP protocol is a transport layer protocol, mainly to solve the data in the network transmission, and HTTP is the application layer protocol, mainly to solve how to package data. On the relationship between TCP/IP and the HTTP protocol, the network has a relatively easy to understand introduction:

"When we're transmitting data, you can use only the (Transport Layer) TCP/IP protocol, but in that case, if there is no application layer, it can not recognize the data content, if you want to make the data transfer meaningful, you must use the Application layer protocol, Application layer protocol, such as HTTP, FTP, Telnet and so on, You can also define the application layer protocol yourself. The web uses the HTTP protocol as an application layer protocol to encapsulate HTTP text information and then use TCP/IP as a Transport layer protocol to send it to the network. ”

And we usually say most of the socket is what it is, in fact, the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. In fact, there is no inevitable connection between the socket and the TCP/IP protocol. Socket programming interface in the design, it is expected to be able to adapt to other network protocols. So, the appearance of the socket just makes the programmer more convenient to use the TCP/IP protocol stack, 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. The network has a section on the socket and TCP/IP protocol relationship is easy to understand the argument:

"TCP/IP is just a protocol stack, just like the operating system, it has to be implemented, but also to provide external operational interface." This is like the operating system will provide a standard programming interface, such as Win32 programming interface, TCP/IP also to provide for programmers to do network development interface, this is the socket programming interface. ”

Related to the TCP/IP protocol is just, with the broad and profound speaking I think also not for too, just check the internet about this kind of information and books on the number of documents to know, this I intend to buy some classic books (such as "TCP/IP Detailed: Volume I, Volume II, Volume three") to learn, Today we summarize some of the knowledge based on the TCP/IP protocol application and programming interface, which is just a lot of HTTP and socket.

The CSDN has a more descriptive description:

HTTP is a sedan that provides a specific form of encapsulation or display of data;

socket is the engine that provides the ability of network communication.

In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the Transport layer, and the socket itself is not a protocol, as mentioned above, it only provides an interface for TCP or UDP programming.

Here are some of the important concepts that are often encountered in a written test or interview, especially in this excerpt and summary.

What is a TCP connection three times handshake

First handshake: The client sends the SYN packet (SYN=J) to the server, and enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server received the SYN packet, must confirm the customer's SYN (ACK=J+1), but also send a SYN packet (syn=k), that is Syn+ack packet, at this time the server into the SYN_RECV state;
Third handshake: The client receives the server Syn+ack packet, sends the confirmation packet ack (ACK=K+1) to the server, this packet sends completes, the client and the server enters established state, completes three times handshake.
Handshake process in the packet is not included in the data, three times after the handshake, the client and the server began to transfer data officially. 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 disconnect process requires a "four handshake" (the process is not fine, that is, the server and client interaction, the final determination of the disconnect)

two steps to establish a network connection using a socket

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

The connection process between sockets is divided into three steps: Server listening, client requests, connection confirmation.

1, the server listens: The server end socket does not locate the specific client socket, but waits for the connection the State, the real-time monitoring network state, waits for the client's connection request.

2, client request: Refers to the client socket to make a connection request, to connect the target is the server side of the socket. To do this, the socket for the client must first describe the socket of the server to which it is connecting, 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 monitor hears or says that a connection request is received from the client socket, in response to the client socket request, create a new thread, the server-side socket description to the client, once the client confirmed this description, the two sides formally establish a connection. While the server-side socket continues to be listening, it continues to receive connection requests from other client sockets.

three features of HTTP links

HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the common protocols of mobile networking, and HTTP protocol is an application based on TCP protocol.

The most notable feature of HTTP connections is that each request sent by the client requires a server loopback response, and the connection is actively released after the request is completed. The process from establishing a connection to closing a connection is called a "once connection."

the difference between TCP and UDP (most test. I think it's going to be a bad test.

1, TCP is link-oriented, although the insecurity of the network determines how many times the handshake can not guarantee the reliability of the connection, but TCP's three handshake at least (in fact, to a large extent) to ensure the reliability of the connection, and UDP is not connection-oriented, UDP transfer data is not connected with the other party, the data received do not send a confirmation signal, the sender does not know whether the data will be properly received, of course, do not have to resend, so that UDP is connectionless, unreliable data transmission protocol.

2, is also due to the characteristics of 1, so that the cost of UDP more small data transmission rate is higher, because do not have to send and receive data confirmation, so the real-time performance of UDP better.

Know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than the use of UDP QQ transmission files slow, but can not say that QQ communication is not safe, because programmers can manually to UDP data transceiver verification, For example, the sender of each packet number and then verified by the receiver AH what, even if so, UDP because in the package of the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.

Simple Summary:

HTTP protocol: Simple Object Access Protocol, corresponding to the application layer, HTTP protocol is based on TCP connection.

TCP protocol: corresponds to the transport layer.

IP protocol: corresponds to the network layer.


TCP/IP is a Transport layer protocol that mainly addresses how data is transmitted over the network, while HTTP is an application layer protocol that mainly addresses how data is packaged.

socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol.

 

HTTP Connection: HTTP connection is the so-called short connection, that is, the client sends a request to the server side, the server-side response will be disconnected;

Socket Connection: Socket connection is the so-called long connection, theoretically, the client and server end once the connection will not be actively broken; however, due to various environmental factors may be disconnected, such as: Server-side or client host down, network failure, Or there is no data transfer between the two for a long time, the network firewall may disconnect the connection to free up network resources. So when there is no data transmission in a socket connection, the heartbeat message needs to be sent to maintain the connection ~ ~ The specific heartbeat message format is defined by the developer.

WebSocket

WebSocket is a network technology for Full-duplex communication between browsers and servers that HTML5 has started to provide. The WebSocket communication agreement was set as standard RFC 6455,websocketapi by the IETF in 2011.

In the WebSocket API, the browser and the server only need to do a handshake action, then, between the browser and the server to form a fast channel. The data can be transmitted directly between the two.

Now, many Web sites in order to implement the push technology, the technology used is polling. Polling is a browser that sends HTTP request to the server at a specific time interval, such as every 1 seconds, and then returns the latest data to the client by the server. This traditional pattern has the obvious disadvantage that the browser needs to constantly make requests to the server, but the header of HTTP request is very long, the data contained in it may be only a small value, which takes up a lot of bandwidth and server resources.

and the newer technology to do polling effect is comet, using Ajax. However, although this technology can achieve two-way communication, but still need to make a request, and in comet, the widespread adoption of long links, which also consumes a lot of server bandwidth and resources.

In the face of this situation, HTML5 defines the WebSocket protocol, which can save server resources and bandwidth and achieve real-time communication.

Handshake Protocol

In the process of implementing websocket wiring, a websocket connection request is made through the browser and the server responds, which is often referred to as a "handshake" (handshaking).
PS: Later versions are mostly functional extensions, such as using the 7th version of the handshake Protocol also applies to the 8th version of the Handshake protocol.

Browser request

get/http/1.1

Upgrade:websocket

Connection:upgrade

Host:example.com

Origin:null

sec-websocket-key:sn9crrp/n9ndmgdcy2vjfq==

Sec-websocket-version:13

Server response

http/1.1 Switching protocols

Upgrade:websocket

Connection:upgrade

sec-websocket-accept:ffboob7fakllxgrsz0bt3v4hq5s=

Sec-websocket-origin:null

sec-websocket-location:ws://example.com/

principle

The "Sec-websocket-key" in the request is random, and the server side uses the data to construct a SHA-1 summary of the information.

Add "Sec-websocket-key" to a magic string "258eafa5-e914-47da-95ca-c5ab0dc85b11". Use SHA-1 encryption, then BASE-64 encoding, the result as "sec-websocket-accept" the value of the header, returned to the client.

The client's WebSocket object has a total of four events bound:

1. OnOpen: Trigger when connection is established;

2, OnMessage: When the message is received service-side trigger;

3. OnError: Trigger when connection error occurs;

4. OnClose: When the connection is closed

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.