Computer network interview core

Introduction to transport Layer control protocol TCP

  1. Introduction to transport Layer control protocol TCP

    • Connection-oriented, reliable, byte stream – based transport-layer communication protocol
    • The data flow at the application layer is divided into packet segments and sent to the TCP layer of the target node
    • If the packet is received, it will send an ACK for confirmation. If the packet is not received, it will be retransmitted
    • Use checksums to verify that data is being transmitted incorrectly
  2. Header format

TCP Flags: URG: emergency pointer flag 2. ACK: confirm sequence number 3. PSH: push flag 4. RST: rework connection flag 5

TCP three-way handshake

Handshake is used to establish a connection. The flow chart of TCP three-way handshake is as follows:

Question: Tell me about the TCP three-way handshake

Handshake is used to establish a connection. The flow chart of the TCP three-way handshake is as follows: In TCP/IP, TCP provides reliable connection services and uses the three-way handshake to establish a connection.

First handshake: When establishing a connection, the client sends a SYN packet [SYN = J] to the server and enters the SYN_SEND state, waiting for confirmation from the server. Second handshake: After receiving a SYN packet, the server must acknowledge the client’s SYN(ACK = J +1) and send a SYN packet (ACK = K). In this case, the server enters the SYN_ RECV state. Third handshake: After receiving a SYN+ACK packet from the server, the client sends an ACK packet (ACK = K +1) to the server. After the packet is sent, the client and the server access the server

In the ESTABLISHED state, the three-way handshake is complete.

Question: Why does it take three handshakes to establish a connection

To initialize the initial value of the Sequence Number

Question: Defense against SYN Flood attacks

SYN Cookie sent back through tcp_ syncookies

Question: What should I do if the Client fails after the connection is established

Live retaining mechanism: Sending a live probe message to the other party, if no response is received, sending another probe if no response is received after the number of attempts reaches the number of live probe attempts, the connection is interrupted

Talk about TCP’s four waves

The “wave” is to terminate the connection. The flow chart of TCP wave four times is as follows:

Close-wait: The server is half-closed. No data is sent from the client, but the data sent by the server can be received

Question: Talk about TCP’s four waves

The “wave” is used to terminate the connection. The flow chart of TCP wave four times is as follows:

First wave: The Client sends a FIN to stop data transmission from the Client to the Server. The Client enters the FIN_WAIT_1 state. Second wave: After receiving a FIN, the Server sends an ACK to the Client. The ACK sequence number is +1 (the same as that for SYN, one FIN occupies one sequence number). The Server enters CLOSE WAIT state. Third wave: The Server sends a FIN to close data transmission from the Server to the Client. The Server enters the LAST ACK state. Fourth wave: After receiving the FIN, the Client enters the TIME_WAIT state and sends an ACK to the Server to confirm that the FIN number is +1. The Server enters the CLOSED state and waves four times.

Question: Why is there a TIME_WAIT state

  1. Ensure that there is enough time for the ACK packet to be received
  2. Avoid confusion between old and new connections

Question: Why does it take four waves to disconnect

Because of full-duplex, both the sender and the receiver require FIN packets and ACK packets

Question: The reason for the large number of CLOSE_wait states on the server

Preparation of code that is checked, especially the code that releases the resource, and configuration, especially the configuration of the thread that processes the request

Introduction of UDP

Question: UDP features

  1. Oriented disconnection
  2. Does not maintain connection state and supports simultaneous transmission of the same message to multiple clients
  3. The packet header is only 8 bytes and the extra overhead is small
  4. Throughput is limited only by data generation rate, transmission rate, and machine performance
  5. Best effort delivery, no guarantee of reliable delivery, no need to maintain complex linked state tables
  6. Packet oriented, the packet information submitted by the application program is not split or merged

Question: Difference between TCP and UDP

  1. Connection-oriented vs connectionless
  2. reliability
  3. order
  4. speed
  5. Order of magnitude

TCP sliding window

Question: RTT and RTO

Preparation of a packet for receiving an ACK

Question: TCP uses sliding Windows for flow control and out-of-order rearrangement

  1. Ensure TCP reliability
  2. Ensure TCP flow control

Window: indicates the data field that the notification sender can receive

HTTP profile

Question: Main features of HYPERtext transport protocol HTTP

  • Client/server mode is supported
  • Easy/fast
  • flexible
  • There is no connection
  • stateless

Question: HTTP request structure

Request line, request header, blank line, request body

Question: Request/response step

  • The client connects to the Web server
  • Sending an HTTP request
  • The server accepts the request and returns an HTTP response
  • Release the connection TCP connection
  • The client browser parses HTML content

Question: The process of entering the URL in the browser address bar and pressing Enter

  1. The DNS
  2. A TCP connection
  3. Sending an HTTP request
  4. The server processes the request and returns HTTP packets
  5. The browser parses the rendered page
  6. Connect the end of the

Question: INDICATES the HTTP status code

2xx: Successful — indicating that the request has been received, understood or accepted. 3XX: Redirected — a further operation must be performed to complete the request : server side error – The server failed to fulfill a valid request

Question: Common status code

400 Bad Request: The client Request has a syntax error and cannot be understood by the server. 401 Unauthorized: The Request is not authorized. 403 Forbidden: The server received the request but refused to provide service 404 Not Found: The requested resource does Not exist, eg, An incorrect URL is entered. 500 Internal Server Error: An unexpected Error occurs on the Server. 503 Server Unavailable: The Server cannot process requests from the client

Question: Difference between A GET request and a POST request

Abstract: The CET is idempotent and secure, whereas the POST is not. The OTHER level is: THE GET can be cached and stored, but the POST cannot

Question: Difference between Cookie and Session

Cookie Introduction: It is a special message sent by the server to the client, which is stored on the client in the form of text

The server side mechanism, the information saved on the server: The client request is parsed and the Session ID is acted on, saving the status information as needed

: : Session data stored on the client’s browser; Session data stored on the server

Differences between HTTP and HTTPS

Question: SSL(Security Sockets Layer)

Designed to provide security and data integrity for Network communication. This is an API for an operating system

Question: Encryption mode

Asymmetric encryption: The encryption key used is different from the decryption key

Question: HTTPS data transfer process

➢ the browser will support encryption algorithm of information sent to the server ➢ server selection a browser support encryption algorithm, in the form of a certificate of browser postback ➢ browser validation certificate of legitimacy, and connecting with the certificate of public key encryption information sent to the server ➢ server using a private key to decrypt the message, verify hash, encrypt the response message back to the browser Trained web browser to decrypt the response message, verify the truth of the message, and encrypt the interactive data

Question: Difference between HTTP and HTTPS

Such as: Port 443 by default for HTTPS, port 80 for HTTP

Question: Is HTTPS really secure

That is not necessarily the browser filling http:// by default, the request needs to be jumped, there is a risk of being hijacked, can be optimized using HSTS (HTTP Strict Transport Security)

Introduction of the Socket

A Socket is an abstraction of TCP/IP and an interface open to the operating system

Socket Communication Flow

Question: Writing a web application, the client and the server, the client to the server sends a string, the server receives the string after print it on the command line, and then returned to the client that the length of the string, in the end, the client output the length of the string returned by the server, respectively with TCP and UDP two ways to achieve them

public class LengthCalculator extends  Thread{
    private Socket socket;

    public LengthCalculator(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run(a){
        try{
            OutputStream os =socket.getOutputStream();
            InputStream is =socket.getInputStream();
            int ch=0;
            byte[] buff =new byte[1024];
            ch=is.read(buff);
            String content =new String(buff,0,ch);
            System.out.println(content);
            os.write(String.valueOf(content.length()).getBytes());
            is.close();
            os.close();
            socket.close();
        } catch(IOException e) { e.printStackTrace(); }}}public class TCPClient {
    public static void main(String[] args) throws IOException {
        Socket socket =new Socket("127.0.0.1".65000);
        OutputStream os =socket.getOutputStream();
        InputStream is =socket.getInputStream();
        os.write(new String("hello world").getBytes());
        int ch=0;
        byte[] buff =new byte[1024];
        ch=is.read(buff);
        String content =new String(buff,0,ch); System.out.println(content); is.close(); os.close(); socket.close(); }}public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss =new ServerSocket(65000);
        while(true){
            Socket socket=ss.accept();
            newLengthCalculator(socket).start(); }}}public class UDPClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket =new DatagramSocket();
        byte[] buf ="hello world".getBytes();
        // Encapsulate the IP as an InetAddress object
        InetAddress address =InetAddress.getByName("127.0.0.1");
        // Encapsulate the data to be sent into a DatagramPacket object, fill in the IP address and port number
        DatagramPacket packet =new DatagramPacket(buf,buf.length,address,65001);
        socket.send(packet);

        byte[] data =new byte[100];
        // Create a DatagramPacket object to store data sent by the server
        DatagramPacket receivedPacket = new DatagramPacket(data,data.length);
        socket.receive(receivedPacket);
        // Take the data out and print it
        String content = new String(receivedPacket.getData(),0,receivedPacket.getLength()); System.out.println(content); }}public class UDPServer {
    public static void main(String[] args) throws IOException {
        // The server receives the datagram sent by the client
        DatagramSocket socket =new DatagramSocket(65001);
        // Store the content received from the client
        byte[] buff =new byte[100];
        // Receives the content sent by the client and encapsulates the content into a DatagramPacket object
        DatagramPacket packet =new DatagramPacket(buff,buff.length);
        socket.receive(packet);
        // Retrieve the actual stored data from the DatagramPacket
        byte[] data =packet.getData();
        // Convert data from binary to string form
        String content =new String(data,0,packet.getLength());
        System.out.println(content);
        // Change the data to binary to be sent to the client
        byte[] sendedContent =String.valueOf(content.length()).getBytes();
        // The server sends datagrams to the client
        // Obtain the source address and port number of the DatagramPacket object
        DatagramPacket packetToClient =newDatagramPacket(sendedContent,sendedContent.length,packet.getAddress(),packet.getPort()); socket.send(packetToClient); }}Copy the code