iOS interview questions - internal strength -socket

What is a socket?

1. Background: Speaking socket, we need to look at the background of its birth, assuming a situation: we want a different process for network communications, then we need each process needs to have a unique identity, but in a different PID operating system above process may duplicate the problem, which is not feasible, in order to solve this problem, TCP / IP protocol came in handy, IP protocol can only represent one computer, protocol type, and TCP in port can only indicate a computer process thus achieving network communication problems between different processes of different computers. Then there is a problem is that every time we need to write the code in accordance with the process-oriented in development time, in order to solve this problem, Socket born friends;
2, concept: it is located on TCP / IP protocol on the basis of a layer, is computer cornerstone in communication, the basic operation unit TCP / IP protocol suite, a socket requires a necessary element: the protocol type is UDP port or the TCP, the source host address, the source host address process, destination host address, destination host process port; protocol, but it is not a TCP / IP-encapsulated TCP / UDP provides a type of an upper layer based on a c api interfaces;
. 3, Socket divided into two categories, when the type of the byte stream data, a current TCP protocol is the socket socket; when a user packet, a current socket UDP protocol is the socket,
3.1, TCP:
TCP is a connection-oriented, high reliability of the communication protocol of the transport layer:
connection-oriented: because the TCP will be carried out before the three-way handshake for communication, will be disconnected four break;
high reliability: because TCP network environment will be owned Control, flow control;
3.2, congestion control, flow control, retransmission mechanism?

First we have to until under what circumstances, the need for congestion control, traffic control? , Congestion control and flow control principle?

拥塞控制:  我们在进行网络通信的时候需要用到网络资源,网络资源包括数据包缓存区、网络带宽、路由节点的处理能力;在某段时间内,若对网络中某一资源的需求超过了该资源能够提供的可用部分时,网络性能就会变坏,这种情况就叫做拥塞;拥塞发生的主要原因是网路能够提供可用资源不足以满足用户的需求;
产生拥塞控制过程: 在早期TCP发送数据包,一致直接发送最大数据量的包,这样很快,网络层的带宽资源、接收节点也处黎不过来,甚至导致网络层崩溃,后期为了解决这个问题引进了嗅探发送,就是刚开始讲自己的拥塞窗口大小设为最大的发送数据即MMS,假设为M;刚开始按照指数级发送数据包,当发送的数据包数量大于M,此时会采用拥塞控制算法,重启从0开始,按线性增大发送数据,并且拥塞窗口的大小为原来一般即M/2,后面一次照这样发送,这期间主要如果服务端没有收到某个数据包,此时服务端会连续向客户端发送3次ACK包请求具体的包,这时,客户端会立即开始快速重传

TCP: Details
https://www.jianshu.com/writer#/notebooks/37246528/notes/43805129

3.2, the DUP
the UDP is a connectionless and unreliable, based on the data reported, the transport layer of the communication protocol.

3.3, TCP / UDP disadvantages:
TCP advantage: three-way handshake test, confirmation, windows, retransmission, congestion control mechanisms, the process of the transfer is complete disconnected, to release resources.
TCP drawback: the validation phase has been attacked, low efficiency, CPU, memory and other hardware resources occupied by the system, not suitable for small amount of data transferred, overkill.
TCP advantages: High Efficiency
TCP disadvantages: unreliable, not suitable for large data transfers.
Summary of the difference between UDP and TCP:
1. Based linked to a connectionless;
2. requirements for system resources (TCP more, less UDP);
3.UDP relatively simple program structure;
4. stream mode and packet mode data;
5. TCP guarantees data correctness, UDP packet loss may, TCP order to ensure that data, UDP is not guaranteed.

4, the process works:
4.1, the server creates a socket, bind server host address, a port with shoulder pain, then call accept function, consistent started blocking state; when the client has a client requests, and blocking their entry into, until the service returns data; the server save the client socket, then the socket is written to a single customer data; the client receives the message can send a close link is disconnected, then complete a complete exchange of the socket

Guess you like

Origin blog.csdn.net/weixin_34205076/article/details/90922050