Key knowledge (to create a web server and TCP) to resolve

Key knowledge (to create a web server and TCP) to resolve (a)

1. Create a simple TCP server processes ?

  • Socket Socket Create -> Binding Ip address and port number -> Set up listeners listen-> Accept Connection Request Wait -> Receive Data -> transmit data -> closed sleeve bytes
  • Client: create socket -> Request Connection -> accept and process data -> transmit data -> Close the socket

2. Talk about ⼯ the socket send and recv for the principle.

  • Socket socket, is the way of inter-process communication network, there are tcp and udp two protocols
  • The socket is to send and receive data by calling two functions send and recv
  • However, the function does not send the data directly to the web server, wants to send data to the web server, the application must call the operating system interface, data is sent by the operating system through the network card, and send only send data to the transmit buffer (memory the eleven space), and then call the operating system interface by the application, send the card data is controlled by the operating system.
  • Of course, recv is not available directly from the web server data directly, in order to receive the data sent by the server through the network card must, that is, the operating system receives data through the network card and sends the data to the receive buffer, then the application by socket socket from the receive buffer to accept data in to the application.

3. talk about what is the difference between UDP and TCP?

  • TCP : connection-oriented: connection requires three-way handshake to disconnect data transmission requires four waving, UDP : the need to connect, send data directly, no matter if it was received
    TCP : safe - reliable, the data will be retransmission timeout, error checking, flow control and congestion control, to ensure data security, and UDP : does not guarantee the accuracy of the data
    TCP : connection-oriented, need more resources, more slowly, while the UDP without connecting, fast, available to broadcast

3.1 three-way handshake analysis, why it must be three times instead of twice, four times what?

- 第一次握手  -  client 发送一个syn(序号)包(seq=x)(这个包里会带一个序列号)到服务器,等待服务器确认,然后客户端进入syn_send状态.
- 第二次握手 - 服务器收到client客户端的syn包,必须要确认客户的syn ,所以要对syn进行(seq=x+1)操作,同时也会发送一个自己的syn包(seq=y).然后服务器会进入syn_recv状态.
- 第三次握手 - 客户端client收到服务器发来的syn包时,会对其进行确认,如果有效,则将syn包seq=y+1发送给服务器,告诉他,我客户端收到了你的包.
- 自此连接就已完全的建立好了...
-**为什么不能是两次呢?**
- --
```有一种应用场景,可能由于网络原因,当客户端发送syn包时,服务端并没有及时收到,而客户端一直处于syn_send状态,又因为传输基于tcp协议,具有超时重传的功能,所以在长时间等待中,客户端根据超时重传的算法进行重新发送,这时候服务端收到syn包,并开始连接,与此同时,第一次发送的syn包(之前因为网络原因)也抵达到了服务端,创建连接,但此连接是无任何响应的, 所以是死连接```
由于一个进程能够创建的socket数量有上限,所维护这些死连接非常的耗费资源
**总结:**
	简单来说,三次握手的过程,server和client都有一发送syn和接收ack的过程,双方都能够发送数据和接收数据,彼此互相通信,没必要在增加第四次握手

3.2 Why establish a connection is disconnected three times and is four times?

  • Because when you create a connection, the client sends a request to establish a client to server connection SYN packet, when the server receives the request, SYN and services will be sent by the client end its ACK in a packet, please accept client (client) once sent in the past.
  • When disconnected, when the server receives a client request to disconnect FIN packets, it does not bring the FIN ACK request and on their own to send the same message, because it is possible the client's own needs or are this client data transmission can not be disconnected from this client immediately, so FIN packets and ACK packets are sent separately this time, it is disconnected from the required four times.

4.TCP is long or short connection connection

  • TCP, both long connection, there are short link
    • 短连接:当tcp三次握手建立连接后,通信一次,就四次挥手,断开连接
    • 长连接: 当tcp三次握手建立连接 ,就一直不断开连接
Published 11 original articles · won praise 9 · views 227

Guess you like

Origin blog.csdn.net/lzn1210899799/article/details/103758491