--- --- restore content begins
OSI & TCP / IP model
To clarify the tcp
udp
socket
http
websocket
relationship between the first to know the classic OSI
seven-layer model, the corresponding is TCP/IP
a four-layer model.
OSI model includes seven layers, while the TCP / IP model has only four. Although they have a function corresponding to the network layer, transport layer and application layer, but other layers are not the same.
Network data transmission we generally use the bottom seven floors, respectively, the physical layer, data link layer, network layer, transport layer, session layer, presentation layer, application layer, also referred to as a first layer of the OSI are sequentially, The second layer, ⋯⋯, seventh layer.
TCP / IP model no specialized session layer and the presentation layer, it will be associated with the expression of these two layers, the coding and session control functions to the application layer contains to complete. In addition, TCP / IP also OSI model and the data link layer to the physical layer comprises a network access layer.
In the OSI model network layer supports connectionless and connection-oriented two services, and the transport layer only supports connection-oriented services. TCP / IP layer of the Internet model only supports a connectionless service, the transport layer connection-oriented and connectionless support both services.
TCP / IP due to fewer levels, and thus appear to be more simple, and as the de facto standard for development on the Internet (INTERNET) up agreement, it has become a network interconnection. However, there is no actual network is built on the basis of the OSI model, only the OSI reference model is widely used as a theory.
As can be seen from the figure, tcp
udp
the work of the transport layer, http
websocket
work in the application layer, but socket
does not belong to any of the seven layer model, can be understood as socket
work in the hidden layer and the intermediate transport layer to the application layer.
The relationship between several persons
socket
Itself is not a protocol, but the transport layer TCP/UDP
protocol is encapsulated, hidden from the user inside TCP/UDP
how the transmission, only provides an interface (API) calls to the programmer, to complete the socket
programming. Through socket
the interface, we can use the TCP/UDP
protocol.
About socket
the TCP/UDP
relationship similar to the relationship between the operating system and the interface: TCP/IP
just a protocol stack, as the operating system of the operating mechanism, it must be specific implementation, but also provide external operator interface. This is like the operating system will provide standard programming interfaces, such as win32 programming interface, TCP/IP
but also provides an interface for programmers to do web development used, which is the Socket
programming interface.
And we usually visit the website used http
protocol is based TCP
on the protocol of an application layer protocol. http
The most notable feature of the protocol is that it is a non-connected state, the client sends the server needs to send back a response for each request, after the request, will automatically release the connection, a "short connection."
Overview of the relationship between several persons
The relationship between the few who can be summarized as follows:
http
websocket
Application layer protocol socket
interfaces to invoke tcp
udp
other transport layer protocol, so as to achieve a communication network.
tcp
udp
=》 socket
=》 http
websocket
to sum up
In summary, we are not programmed to call directly tcp
udp
, but through their packaged interfaces socket
to communicate. We can say that now almost all communication on the network, through the bottom is socket
done, everything is Socket
.
supplement. . .
The difference between tcp and udp
TCP is connection-oriented, reliable transmission (data to ensure data accuracy and to ensure that order), for transmitting large amounts of data (stream mode), slowly, establishing a connection requires more overhead (time and system resources).
TCP是一种流模式的协议,是面向连接的,也就是说,在连接持续的过程中,Socket 中收到的数据都是由同一台主机发出的(劫持什么的不考虑),因此,知道保证数据是有序的到达就行了,至于每次读取多少数据不关心。
TCP:面向连接、传输可靠(保证数据正确性,保证数据顺序)、用于传输大量数据(流模式)、速度慢,建立连接需要开销较多(时间,系统资源)。
UDP:面向非连接、传输不可靠、用于传输少量数据(数据包模式)、速度快。
关于TCP是一种流模式的协议,UDP是一种数据报模式的协议,这里要说明一下,TCP是面向连接的,也就是说,在连接持续的过程中,socket 中收到的数据都是由同一台主机发出的(劫持什么的不考虑),因此,知道保证数据是有序的到达就行了,至于每次读取多少数据自己看着办。
而UDP是无连接的协议,也就是说,只要知道接收端的IP和端口,且网络是可达的,任何主机都可以向接收端发送数据。这时候,如果一次能读取超过一个报文的数据,则会乱套。比如,主机A向发送了报文P1,主机B发送了报文P2,如果能够读取超过一个报文的数据,那么就会将P1和P2的数据合并在了一 起,这样的数据是没有意义的。
TCP三次握手
所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包。
三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号并交换 TCP 窗口大小信息.在 Socket 编程中,客户端执行connect()时。将触发三次握手。
首先了解一下几个标志,SYN(synchronous),同步标志,ACK (Acknowledgement),即确认标志,seq应该是Sequence Number,序列号的意思,另外还有四次握手的fin,应该是final,表示结束标志。
The first handshake: a client sends a SYN flag of the TCP packet indicates a position of the client wants the server port connection, and an initial sequence number X, is stored in the header of the sequence number (Sequence Number) field.
The second handshake: server sends back an acknowledgment packet (ACK) response. I.e., the SYN flag bit and the ACK flag bit are both 1 at the same time, the acknowledgment sequence number (Acknowledgement Number) is set to 1 plus the sequence number of customers in, i.e., X + 1.
Third handshake: a client sends an acknowledgment packet (ACK) SYN flag bit is 0, ACK flag bit to 1 again. And the sequence number field + ACK is sent from the server, determines in the field send to each other. Put written and the sequence number + 1 in the data segment.
There are two common types Socket: Socket stream (SOCK_STREAM) and Datagram Socket (SOCK_DGRAM). Streaming is a connection-oriented Socket for service application in connection-oriented TCP; Datagram Socket Socket is a connectionless, corresponding to UDP is connectionless services.
--- end --- restore content