TCP, UDP, Socket, HTTP network programming interview questions (summarize the most comprehensive interview questions!!!)

TCP, UDP, Socket, HTTP network programming interview questions (summarize the most comprehensive interview questions!!!)

Summary of Java interviews, including key knowledge of Java and commonly used open source frameworks. Welcome everyone to read. There may be errors in the article, because personal knowledge is limited, you are welcome to point out! The article continues to be updated...

ID title address
1 Design pattern interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
2 Java basic knowledge interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
3 Java collection interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
4 JavaIO, BIO, NIO, AIO, Netty interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
5 Java concurrent programming interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
6 Java exception interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
7 Java Virtual Machine (JVM) Interview Questions (Summary of the most comprehensive interview questions) juejin.im/post/684490...
8 Spring interview questions (summarizing the most comprehensive interview questions) juejin.im/post/684490...
9 Spring MVC interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
10 Spring Boot interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
11 Spring Cloud interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
12 Redis interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
13 MyBatis interview questions (summarizing the most comprehensive interview questions) juejin.im/post/684490...
14 MySQL interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
15 TCP, UDP, Socket, HTTP interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
16 Nginx interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
17 ElasticSearch interview questions
18 Kafka interview questions
19 RabbitMQ interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
20 Dubbo interview questions (summarizing the most comprehensive interview questions) juejin.im/post/684490...
21 ZooKeeper interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
22 Netty interview questions (summarize the most comprehensive interview questions)
23 Tomcat interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
24 Linux interview questions (summarize the most comprehensive interview questions) juejin.im/post/684490...
25 Internet-related interview questions (summarizing the most comprehensive interview questions)
26 Internet Security Interview Questions (Summary of the most comprehensive interview questions)

Let's look at the experience of a day's interview:

First round:

  • Interviewer: Tell me about the three-way handshake of TCP
  • Me: The first time Client sets SYN to 1..., the second time Server receives..., the third time...
  • Interviewer: Is it hard to recite?
  • Me: ...Yes, it's difficult, or I'm telling you about wave four times?
  • Interviewer: Don t stop, don t go back and wait for the notice...
  • I:"......"

second round:

  • ....
  • Interviewer: Tell me about the three-way handshake of TCP
  • Me ( ): There is nothing to say, just to maintain a normal network communication interaction
  • Interviewer: Can you make it clearer?
  • Me: It means you called me for an interview without knowing me
  • Interviewer: "I'm confused", it seems like this
  • Interviewer: Tell me about the four waves of TCP
  • Me: It's equivalent to my resignation at my last company
  • Interviewer: "Think about it", can you make it clearer?
  • Me: I asked the boss to resign, and the boss said yes, the boss then resigned for me, so I can leave
  • Interviewer: Makes sense!
  • Interviewer: Tell me about the difference between TCP and UDP
  • Me: TCP is equivalent to calling a stranger to deal with things, UDP is equivalent to broadcasting
  • Interviewer: "..." makes sense
  • Interviewer: what salary do you expect
  • Me: 15K
  • Interviewer: Have time to start next Monday?
  • I:....

What is network programming

  • The essence of network programming is data exchange between multiple computers. The data transmission itself is not very difficult. It is not just sending the data in one device to other devices, and then accepting the data feedback from another device. Current network programming is basically based on the request/response approach, that is, one device sends request data to another, and then receives feedback from the other device. In network programming, the program that initiates the connection, that is, the program that sends the first request, is called the client (Client), and the program that waits for other programs to connect is called the server (Server). The client program can be started when needed, and the server needs to be started all the time in order to be able to connect at all times.

  • Take a call as an example. The person who dials first is similar to the client, and the person who answers the call must keep the phone open, similar to the server. Once the connection is established, the client and server can transfer data, and the identities of the two are equivalent. In some programs, the program has both client-side and server-side functions. The most common software is QQ and WeChat.

Two main problems in network programming

  1. One is how to accurately locate one or more hosts on the network,
  2. The other is how to transfer data reliably and efficiently after finding the host.
  • In the TCP/IP protocol, the IP layer is mainly responsible for the positioning of the network host, the routing of data transmission, and the IP address can uniquely determine a host on the Internet.

  • The TCP layer provides an application-oriented reliable (TCP) or unreliable (UDP) data transmission mechanism, which is the main object of network programming, and generally does not need to care about how the IP layer processes data.

  • The current more popular network programming model is the client/server (C/S) structure. That is, one of the communication parties acts as a server to wait for the client to make a request and respond. Clients apply to the server when they need services. The server generally runs as a daemon process and monitors the network port. Once a client requests it, it will start a service process to respond to the client, and at the same time continue to monitor the service port, so that subsequent clients can also get services in time.

What is the network protocol

  • To exchange data in an orderly manner in a computer network, it is necessary to comply with some pre-agreed rules, such as the format of the exchanged data and whether a response message needs to be sent. These rules are called network protocols.

Why layer network protocols

  • Simplify the difficulty and complexity of the problem. Since the layers are independent, we can divide big problems into small ones.
  • Good flexibility. When the technology of one layer changes, as long as the interface relationship between the layers remains unchanged, the other layers are not affected.
  • Easy to implement and maintain.
  • Promote standardization work. After separation, the function of each layer can be described relatively simply

Computer network architecture

  • OSI Reference Model
    • OSI (Open System Interconnect), that is, open system interconnection. It is generally called the OSI reference model, which is a network interconnection model researched by the ISO (International Organization for Standardization) organization in 1985. In order to better make network applications more popular, ISO has launched the OSI reference model, so that all companies can specify their own networks in accordance with a unified standard, and they can communicate with each other.
    • OSI defines the seven-layer framework of network interconnection (physical layer, data link layer, network layer, transport layer, session layer, presentation layer, application layer).

  • TCP/IP reference model

  • TCP/IP four-layer protocol (data link layer, network layer, transport layer, application layer)

    1. Application layer The application layer is the layer closest to the user, which provides application interfaces for computer users and also provides users with various network services directly. Our common application layer network service protocols are: HTTP, HTTPS, FTP, TELNET, etc.
    2. The transport layer establishes an end-to-end link from the host. The role of the transport layer is to provide end-to-end reliable and transparent data transmission services for upper-layer protocols, including handling issues such as error control and flow control. This layer shields the details of the lower-layer data communication from the upper layer, so that the upper-layer users only see a host-to-host, reliable data path between the two transmission entities that can be controlled and set by the user. We usually say that TCP UDP is at this layer. The port number is the "end" here.
    3. The network layer establishes a connection between two nodes through IP addressing, selects appropriate routing and switching nodes for the packets sent by the transport layer at the source end, and transmits them to the transport layer at the destination end according to the address correctly. It is usually referred to as the IP layer. This layer is what we often call the IP protocol layer. The IP protocol is the foundation of the Internet.
    4. The data link layer controls the transmission of these data through some procedures or protocols to ensure the correctness of the transmitted data. The software that implements these procedures or protocols is added to the physical circuit, which constitutes a data link,

1 TCP/UDP

1.1 What is TCP/IP and UDP

  • TCP/IP stands for Transmission Control/Network Protocol. It is a connection-oriented protocol. It is necessary to establish a connection before sending data (a connection must be established between the sender and the receiver). TCP provides reliable services, that is Say, the data transmitted through the TCP connection will not be lost, there is no duplication, and arrive in order

  • UDP is a kind of TCP/IP protocol family. It is a connectionless protocol, and there is no need to establish a connection before sending data. It is a protocol without reliability. Because there is no need to establish a connection, it can be transmitted on any possible path on the network, so whether it can reach the destination, the time to reach the destination, and the correctness of the content cannot be guaranteed.

1.2 The difference between TCP and UDP:

  • TCP is a connection-oriented protocol. The connection must be established before sending data. TCP provides reliable services, that is, the data transmitted through the TCP connection will not be lost, repeated, and arrive in order;

  • UDP is a connectionless protocol, there is no need to establish a connection before sending data, it is not reliable;

  • TCP communication is similar to making a phone call, connecting it, and confirming the identity before it starts to pass;

  • UDP communication is similar to school broadcasting, and it communicates directly by broadcasting.

  • TCP only supports point-to-point communication, UDP supports one-to-one, one-to-many, many-to-one, and many-to-many;

  • TCP is byte-oriented flow, UDP is message-oriented; byte-oriented flow means that when sending data in bytes, a data packet can be divided into several groups for transmission, while UDP can only send a message once Send out.

  • TCP header overhead (20 bytes) is larger than UDP header overhead (8 bytes)

  • The UDP host does not need to maintain a complicated connection state table

1.3 Application scenarios of TCP and UDP:

  • UDP is used for some situations with high real-time requirements, such as games, media communications, and real-time live broadcasts. Even transmission errors can be tolerated; in most other cases, HTTP uses TCP because the transmission content is required to be reliable. Lost

1.4 Describe TCP and UDP

  • TCP communication can be regarded as a phone call:

    Li San (dial a number): Hello, is it Wang Wu? Wang Wu: Hey, who are you? Li San: This is Li San. I want to tell you something. Is it convenient for you now? Wang Wu: Oh, it's convenient for me now, just say it. A: What did I say? B: Tell me. (The connection is established, the next step is to talk about business...)

  • UDP communication can be seen as a broadcast in the school:

    Broadcasting room: Hey hey hey! Full gymnastics gathering

1.5 Application layer protocol analysis running on TCP or UDP.

  • Protocol running on TCP protocol:

    • HTTP (Hypertext Transfer Protocol), mainly used for ordinary browsing.
    • HTTPS (HTTP over SSL, Secure Hypertext Transfer Protocol), the secure version of the HTTP protocol.
    • FTP (File Transfer Protocol) is used for file transfer.
    • POP3 (Post Office Protocol, version 3, Post Office Protocol), used for receiving mail.
    • SMTP (Simple Mail Transfer Protocol) is used to send e-mails.
    • TELNET (Teletype over the Network, network telex), through a terminal (terminal) to log in to the network.
    • SSH (Secure Shell, used to replace the poorly secured TELNET), used for encrypted secure login.
  • Protocol running on UDP protocol:

    • BOOTP (Boot Protocol), applied to diskless devices.
    • NTP (Network Time Protocol) is used for network synchronization.
    • DHCP (Dynamic Host Configuration Protocol, dynamic host configuration protocol), dynamically configure the IP address.
  • Run on TCP and UDP protocol:

    • DNS (Domain Name Service) is used to complete address lookup and mail forwarding.
    • ECHO (Echo Protocol, rewinding protocol), used to check errors and measure response time (running on TCP and UDP protocols).
    • SNMP (Simple Network Management Protocol) is used for network information collection and network management.
    • DHCP (Dynamic Host Configuration Protocol, dynamic host configuration protocol), dynamically configure the IP address.
    • ARP (Address Resolution Protocol, address resolution protocol), used to dynamically resolve the address of the Ethernet hardware.

What is the ARP protocol (Address Resolution Protocol)?

  • The ARP protocol completes the mapping between IP addresses and physical addresses . Each host has a ARP cache, there are a local area network where the IP addresses of the hosts and routers on the map to hardware addresses. When the source host wants to send a data packet to the destination host, it will first check whether there is the MAC address of the destination host in its ARP cache. If so, it will directly send the data packet to this MAC address. If not, it will send it to the destination host . The local area network initiates an ARP request broadcast packet (when sending its own ARP request, it will also bring its own IP address to the hardware address mapping), and the host receiving the request checks whether its own IP address is consistent with the IP address of the destination host If they are consistent, first save the source host's mapping to its own ARP cache, and then send an ARP response packet to the source host. After the source host receives the response packet, it first adds the mapping between the destination host's IP address and the MAC address, and then performs data transmission. If the source host has not received a response, it means that the ARP query has failed.

  • If the host you are looking for is not on the same LAN as the source host, you must find the hardware address of a router on this LAN through ARP, then send the packet to this router, and let this router forward the packet to the next one. The internet. The rest of the work is done by the next network.

What is NAT (Network Address Translation, Network Address Translation)?

  • It is used to solve the problem that the host in the intranet needs to communicate with the host on the Internet. The NAT router converts the host's local IP address to a global IP address, which is divided into static conversion (the global IP address obtained by the conversion is fixed) and dynamic NAT conversion.

The process from entering the address to getting the page?

  1. The browser queries DNS and obtains the IP address corresponding to the domain name: the specific process includes the browser searches its own DNS cache, searches the DNS cache of the operating system, reads the local Host file, and queries the local DNS server. For query to the local DNS server, if the domain name to be queried is contained in the local configuration zone resource, the resolution result is returned to the client to complete the domain name resolution (this resolution is authoritative); if the domain name to be queried is not zoned by the local DNS server Resolve, but the server has cached the URL mapping relationship, then call the IP address mapping to complete the domain name resolution (this resolution is not authoritative). If the local domain name server does not cache the URL mapping relationship, it will initiate a recursive query or iterative query according to its settings;
  2. After the browser obtains the IP address corresponding to the domain name, the browser requests the server to establish a link and initiates a three-way handshake;
  3. After the TCP/IP link is established, the browser sends an HTTP request to the server;
  4. The server receives this request, maps it to a specific request processor for processing according to the path parameter, and returns the processing result and the corresponding view to the browser;
  5. The browser parses and renders the view. If it encounters references to static resources such as js files, css files, and pictures, repeat the above steps and request these resources from the server;
  6. The browser renders the page according to the requested resources and data, and finally presents a complete page to the user.

1.6 TCP three-way handshake

1.6.1 What is TCP's three-way handshake
  • In network data transmission, the transport layer protocol TCP is a reliable transmission to establish a connection. The process of TCP establishing a connection is called a three-way handshake.
1.6.2 Specific details of the three-way handshake

  1. The first handshake: The Client sets SYN to 1, randomly generates an initial sequence number seq and sends it to the Server, and enters the SYN_SENT state;
  2. The second handshake: After the Server receives the Client's SYN=1, knows that the client requests to establish a connection, sets its own SYN to 1, ACK to 1, generates an acknowledge number=sequence number+1, and randomly generates its own initial Serial number, sent to the client; enter the SYN_RCVD state;
  3. The third handshake: The client checks whether the acknowledge number is the serial number +1 and the ACK is 1, and after the check is correct, it sets its own ACK to 1, generates an acknowledge number = the serial number sent by the server +1, and sends it to the server; Enter the ESTABLISHED state; after the server checks that the ACK is 1 and the acknowledge number is the sequence number +1, it also enters the ESTABLISHED state; the three-way handshake is completed, and the connection is established.
  • Simply put:
    1. The client sends SYN to the server
    2. The server returns SYN, ACK
    3. Client sends ACK
1.6.3 Use reality to understand the specific details of the three-way handshake
  • The purpose of the three-way handshake is to establish a reliable communication channel, and the main purpose is to confirm that the sending and receiving of themselves and the other party are normal.
  1. The first handshake: the client cannot confirm anything; the server confirms that the other party is sending normally
  2. The second handshake: The client confirmed: the sending and receiving by itself is normal, and the sending and receiving by the other party are normal; the server confirms: the receiving is normal by itself, and the other party is sending normally
  3. The third handshake: The client confirmed: the sending and receiving by itself is normal, the sending and receiving by the other party is normal; the server confirmed: the sending and receiving by itself is normal, and the sending and receiving by the other party is normal, so the three-way handshake can confirm that the dual sending and receiving functions are normal, and one is missing Not possible.
1.6.4 Can two handshake be established to establish a connection? why?
  • Not possible.
  • Because it may appear that the invalid connection request segment is transmitted to the server. > The first connection request message segment sent by the client was not lost, but stayed at a certain network node for a long time, so that it was delayed until a certain time after the connection was released before reaching the server. Originally, this is a segment that has long since expired. However, after the server receives this invalid connection request segment, it mistakes it for a new connection request sent by the client again. So a confirmation segment is sent to the client, agreeing to establish a connection. Assuming that the "three-way handshake" is not used, then as long as the server sends an acknowledgment, a new connection is established. Since the client does not send a request to establish a connection, it ignores the server's confirmation and does not send data to the server. But the server thinks that the new transport connection has been established, and has been waiting for the client to send data. In this way, a lot of server resources are wasted in vain. Using the "three-way handshake" approach can prevent the above phenomenon from happening. For example, in the situation just now, the client will not send a confirmation to the server's confirmation. Since the server cannot receive the confirmation, it knows that the client did not request to establish a connection.
  • Moreover, the two handshake cannot guarantee that the client correctly receives the message of the second handshake (the Server cannot confirm whether the client has received it), nor can it guarantee the successful exchange of initial serial numbers between the client and the server.
1.6.5 Can a four-way handshake be used? why?
  • This is definitely ok. The three-way handshake can ensure that the connection is successful, let alone four times, but it will reduce the transmission efficiency.
1.6.6 In the third handshake, what happens if the client's ACK is not delivered to the server?
  • Server side: Since the server does not receive the ACK confirmation, it will retransmit the previous SYN+ACK every 3 seconds (the default is five times, and then automatically close the connection and enter the CLOSED state), and the client will retransmit the ACK to the server after receiving it .
  • On the Client side, there will be two situations:
    1. During the server's timeout retransmission process, if the Client sends data to the server, the ACK of the data header is 1, so the server will read the ACK number after receiving the data and enter the establish state
    2. After the Server enters the CLOSED state, if the Client sends data to the server, the server will reply with an RST packet.
1.6.7 What should I do if a connection has been established, but the client fails?
  • The server resets a timer every time it receives a request from the client. The time is usually set to 2 hours. If it has not received any data from the client for two hours, the server will send a probe segment, and then every 75 Sent once every second. If there is no response after sending 10 probe packets, the server considers the client to be faulty, and then closes the connection.
1.6.8 What is the initial serial number?
  • One party A of the TCP connection randomly selects a 32-bit sequence number (Sequence Number) as the initial sequence number (ISN) of the sent data, such as 1000, and uses the sequence number as the origin to perform the data transmission Number: 1001, 1002... During the three-way handshake, this initial serial number is transmitted to the other party B, so that when data is transmitted, B can confirm which data number is legal; at the same time, when data is transmitted, A returns Each byte received by B can be confirmed. If A receives B's acknowledgement number (acknowledge number) is 2001, it means that the data numbered 1001-2000 has been successfully accepted by B.

1.7 4.Waves of TCP

1.7.1 What are the four waves of TCP
  • In network data transmission, the process of disconnecting the transport layer protocol is called four waves of hands
1.7.2 The specific details of the four waves

  1. Wave for the first time: Client sets FIN to 1, sends a sequence number seq to Server; enters FIN_WAIT_1 state;
  2. The second wave: After the server receives the FIN, it sends an ACK=1, acknowledge number=received serial number+1; enters the CLOSE_WAIT state. At this point, the client has no data to send, but it can still accept data from the server.
  3. Wave for the third time: Server sets FIN to 1, and sends a serial number to Client; enters LAST_ACK state;
  4. The fourth wave: After receiving the FIN from the server, the client enters the TIME_WAIT state; then sets the ACK to 1, and sends an acknowledge number=serial number+1 to the server; after the server receives it, it changes to the CLOSED state after confirming the acknowledge number. No more data is sent to the client. The client also enters the CLOSED state after waiting for 2*MSL (the longest life span of the message segment). Complete four waves.
1.7.3 Use reality to understand the specific details of the three-way handshake TCP's four waved hands
  • Wave four times to disconnect the connection because you want to make sure that all the data has been transferred.
  1. After the conversation between the client and the server is over, if the client wants to end the conversation, he will say to the server: I want to close the connection (first wave)
  2. After receiving the client's message, the server said: Okay, you have to close the connection. (Waving for the second time)
  3. Then the server determines that there is nothing to say to the client, and the server will tell the client that I want to close the connection. (Third wave)
  4. After the client receives the message that the server wants to end the connection, it says: It has received the message that you want to close the connection. (Waves for the fourth time) before closing
1.7.4 Why can't the ACK and FIN sent by the server be combined into three waves (what is the meaning of the CLOSE_WAIT state)?
  • Because when the server receives the client's request to disconnect, there may still be some data that has not been sent. At this time, it will reply ACK first, indicating that it has received the disconnect request. Wait until the data is sent and then send FIN to disconnect the data transmission from the server to the client.
1.7.5 What happens if the server's ACK is not delivered to the client during the second wave?
  • If the client does not receive the ACK confirmation, it will resend the FIN request.
1.7.6 What is the meaning of the client TIME_WAIT state?
  • In the fourth wave of hands, the ACK sent by the client to the server may be lost, and the TIME_WAIT state is used to retransmit the ACK message that may be lost. If the Server does not receive the ACK, it will resend the FIN. If the Client receives the FIN within 2*MSL, it will resend the ACK and wait for 2MSL again to prevent the Server from continuously resending the FIN without receiving the ACK. MSL (Maximum Segment Lifetime) refers to the maximum survival time of a segment in the network. 2MSL is the maximum time required for a transmission and a reply. If the Client does not receive the FIN again until 2MSL, the Client concludes that the ACK has been successfully received and ends the TCP connection.

2 Socket

1 What is Socket

  • The two programs on the network exchange data through a two-way communication connection. One end of this two-way link is called a Socket. Socket is usually used to realize the connection between the client and the server. Socket is a very popular programming interface of the TCP/IP protocol. A Socket is uniquely determined by an IP address and a port number.

  • However, the types of protocols supported by Socket are not only TCP/IP, UDP, so there is no inevitable connection between the two. In the Java environment, Socket programming mainly refers to network programming based on the TCP/IP protocol.

  • Socket connection is the so-called long connection. The client and server need to be connected to each other. In theory, once the connection is established between the client and the server, they will not be actively disconnected, but sometimes network fluctuations are still possible.

  • Socket is biased towards the bottom layer. Generally, Socket is rarely used directly for programming, and the bottom layer of the framework uses Socket more.

2 Socket belongs to the level of the network

  • Socket is the middleware abstraction layer for communication between the application layer and the TCP/IP protocol suite. It is a set of interfaces. In the design mode, Socket is actually an appearance mode , which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is everything. Let Socket organize data to meet the specified requirements. protocol.

3 Socket communication process

  • Based on TCP: The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket, and then connects to the server (connect), if the connection is successful, then the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection, and the interaction ends.

  • Based on UDP: UDP protocol is the abbreviation of User Datagram Protocol and is also used for network data transmission. Although the UDP protocol is an unreliable protocol, sometimes UDP will show greater advantages when data needs to be received faster and smaller errors can be tolerated. My client only needs to send it, and it doesn t matter whether the server can receive it.

4 TCP protocol Socket code example:

,

  1. Server:
package com.test.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

//TCP Socket BIO 
public class BIOServer {
    // main 
    public static void main(String[] args) {
        //1 
        ServerSocket server = null;
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            server = new ServerSocket(8000);
            System.out.println(" 8000 ...");
            while (true){
                socket = server.accept(); //
                System.out.println(" " + socket.getRemoteSocketAddress());
                in = socket.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                //
                while ((len = in.read(buffer)) > 0) {
                    System.out.println(new String(buffer, 0, len));
                }
                //
                out = socket.getOutputStream();
                out.write("hello!".getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
  1. Client:
package com.test.io;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

//TCP Socket 
public class Client01 {
    public static void main(String[] args) throws IOException {
        //socket ip port
        Socket socket = new Socket("127.0.0.1", 8000);
        //socket 
        OutputStream outputStream = socket.getOutputStream();
        //IO 
        System.out.println("TCP/n ");
        while(true){
            byte[] car = new Scanner(System.in).nextLine().getBytes();
            outputStream.write(car);
            System.out.println("TCP Socket ");
            //
            outputStream.flush();
        }
    }
}
 

. The test result is sent successfully:

5 Socket code example of UDP protocol:

  1. Server:
//UDP Socket 
public class Server1 {
    public static void main(String[] args) {
        try {
            //DatagramSocket UDP Socket
            DatagramSocket socket = new DatagramSocket(8888);
            //byte 
            byte[] car = new byte[1024];
            //DatagramPacket  DatagramPacket
            DatagramPacket packet = new DatagramPacket(car, car.length);
            ////DatagramPacket receive() , socket 
            System.out.println(" UDP ");
            socket.receive(packet);
            //packet.getLength 
            int length = packet.getLength();
            System.out.println(" " + new String(car, 0, length));
            socket.close();
            System.out.println("UDP Socket ");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
  1. Client:
//UDP Socket 
public class Client1 {
    public static void main(String[] args) {
        try {
            //DatagramSocket UDP Socket
            DatagramSocket socket = new DatagramSocket(2468);
            //Byte 
            byte[] car = "UDP Socket ".getBytes();
            //InetSocketAddress 
            InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
            //DatagramPacket  DatagramPacket
            DatagramPacket packet = new DatagramPacket(car, car.length, address);
            //send()  
            socket.send(packet);
            System.out.println("UDP Socket ");
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

. The test result is sent successfully:

6 Commonly used classes of Socket

Class name Used for effect
Socket TCP protocol The Socket class works on both the client and server at the same time. All methods are common. This class has three main functions, verifying packet information, initiating a connection (Client), and operating stream data (Client/Server)
ServerSocket TCP protocol ServerSocket is represented as a server. Its main function is to bind and monitor a server port, and "clone/map" a Socket object for each client that establishes a connection. The specific data operations are done through this Socket object. ServerSocket only pays attention to how Establish a connection with the client
DatagramSocket ODP protocol The DatagramSocket class is used to represent a socket for sending and receiving datagram packets.
DatagramPacket ODP protocol The DatagramPacket class is used to represent datagram packets, which are used to implement connectionless packet delivery services.
InetAddress IP+port number Java provides the InetAddress class to represent Internet Protocol (IP) addresses. The InetAddress class does not provide a constructor, but provides the following two static methods to obtain an InetAddress instance:
InetSocketAddress IP+port number When using Socket to connect to the server, the easiest way is to use IP and port directly, but this method is not provided in the Socket class. Instead, the subclass of SocketAddress, InetSocketAddress, is used to realize the creation of IP address + port number and does not rely on anything. protocol.

3. HTTP

What is the Http protocol?

  • The Http protocol is a specification for the reliable transmission of text, pictures, audio, video and other hypertext data between the client and the server. The format is referred to as "hypertext transfer protocol".

  • Http protocol belongs to the application layer, and the first layer accessed by users is http

The difference and application scenarios of Socket and http

  • Socket connection is the so-called long connection. In theory, once the connection between the client and the server is established, it will not be actively disconnected;

  • Socket application scenarios: online games, continuous bank interaction, live broadcast, online video, etc.

  • HTTP connection is the so-called short connection, that is, the client sends a request to the server, and the server responds, the connection will be disconnected and wait for the next connection

  • http application scenarios: company OA services, Internet services, e-commerce, office, website, etc.

What is the request body of http?

  • The HTTP request body is the data that is sent to the server first when we request data. After all, if I send the data to the server, I must first indicate what I want.
  • The HTTP request body is composed of: request line, request header, and request data,
  • Note: GIT request does not have a request body
  1. POST request

  2. GIT request has no request body

What are the response messages of http?

  • The HTTP response message is the data the server returns to us. The request body must be present before the response message.

  • The response message contains three parts of status line, response header field, and response content entity implementation

The difference between http and https?

  • In fact, HTTPS is from HTTP plus encryption (usually SSL secure communication line) + authentication + integrity protection

  • the difference:

    1. http needs to get a CA certificate, and money is needed
    2. The port is different, http is 80, https443
    3. http is a hypertext transfer protocol, information is transmitted in plain text, and https is a secure ssl encrypted transmission protocol.
    4. http and https use completely different connection methods (http connection is very simple and stateless; HTTPS protocol is a network protocol constructed by SSL+HTTP protocol that can carry out encrypted transmission and identity authentication, which is safer than http protocol.)

How HTTPS works

  • 1. 1. HTTP requests the server to generate a certificate, and the client verifies the validity period, legitimacy, domain name of the certificate and the requested domain name, and the public key of the certificate (RSA encryption);
  • 2. After the client passes the verification, it will generate a random number based on the validity of the public key of the certificate, and the random number will be encrypted with the public key (RSA encryption);
  • 3. After the message body is generated, its digest is encrypted with MD5 (or SHA1) algorithm, and the RSA signature is obtained at this time;
  • 4. Send to the server, only the server (RSA private key) can decrypt at this time.
  • 5. The random number obtained by decryption is encrypted with AES as the key (the key at this time is only known by the client and server).

How many steps does a complete HTTP request go through?

HTTP HTTP Web Web 7

  1. Establish a TCP connection

    How to establish a connection, see the above three hands covered

  2. The web browser sends a request line to the web server

    Once the TCP connection is established, the Web browser sends a request command to the Web server . For example: GET/sample/hello.jsp HTTP/1.1.

  3. Web browser sends request header

    After the browser sends its request command, it also sends some other information in the form of header information to the Web server. After that, the browser sends a blank line to notify the server that it has finished sending the header information.

  4. Web server response

    After the client sends a request to the server, the server will send back a response from the client, HTTP/1.1 200 OK, and the first part of the response is the version number of the protocol and the response status code.

  5. Web server sends response header

    Just as the client sends information about itself along with the request, the server also sends data about itself and the requested document to the user along with the response.

  6. Web server sends data to browser

    After the web server sends the header information to the browser, it will send a blank line to indicate the end of the header information. Then, it will send the actual data requested by the user in the format described by the Content-Type response header information .

  7. The web server closes the TCP connection

How are the commonly used HTTP status codes classified, and what are the common status codes?

  • The HTTP status code indicates the returned result of the client's HTTP request, identifies whether the server is processing normally, and indicates an error in the request.

  • Type of status code:

category description
1xx: Indication message-indicates that the request has been received and is being processed
2xx: Success-indicates that the request has been successfully received, understood, and accepted
3xx: Redirect-further action must be taken to complete the request
4xx: Client error-the request has a syntax error or the request cannot be fulfilled
5xx: Server-side error-the server failed to fulfill a legitimate request
  • Common status codes:
status code description
200: The request is processed normally
204: The request is accepted but there are no resources to return
206: The client is only a part of the requested resource, and the server only executes the GET method on the requested part of the resource, and the resource within the range specified by the Content-Range in the corresponding message.
301: Permanent redirect
302: Temporary redirect
303: It has a similar function to the 302 status code, except that it hopes that when the client requests a URI, it can be redirected to another URI through the GET method.
304: When sending a request with conditions, it will be returned when the condition is not met. It has nothing to do with redirection.
307: Temporary redirection, similar to 302, except that the POST method is mandatory
400: The request message syntax is incorrect and the server cannot recognize it
401: Request requires authentication
403: The requested corresponding resource is forbidden to be accessed
404: The server cannot find the corresponding resource
500: Server internal error
503: Server is busy

What are the request methods in the Http protocol

Request method description
GET: Used to request access to resources identified by URI (Uniform Resource Identifier), which can be passed to the server through URL
POST: It is used to transmit information to the server. The main function is similar to the GET method, but the POST method is generally recommended.
PUT: When transferring files, the main body of the message contains the content of the file and saves it to the corresponding URI location.
HEAD: Obtaining the message header is similar to the GET method, except that the message body is not returned, and is generally used to verify whether the URI is> valid.
PATCH: The data transmitted by the client to the server replaces the content of the specified document (partial replacement)
TRACE: Echo the original request message of the client requesting the server for "loopback" diagnosis
DELETE: Deleting a file, contrary to the PUT method, deletes the file corresponding to the URI location.
OPTIONS: Query the HTTP method supported by the corresponding URI.

The difference between GET method and POST method

  • Difference 1 : get focuses on obtaining resources from the server, and post focuses on sending data to the server;

  • Difference 2 : The amount of data transmitted by Get is small, because it is limited by the length of the URL, but it is more efficient; Post can transmit a large amount of data, so only the Post method can be used when uploading files;

  • Difference 3 : get is insecure, because the data sent by the get request is visible on the URL, which may reveal private information, such as passwords, etc.; the post is placed in the request header, which is safe

Comparison of http version

  • Features of HTTP1.0 version:

    • The earlier version of HTTP 1.0 was a stateless, connectionless application layer protocol.
    • HTTP1.0 stipulates that the browser and the server maintain a short-term connection. Each request of the browser needs to establish a TCP connection with the server. The server will immediately disconnect the TCP connection (no connection) after processing. The server does not track each client. Past requests are not recorded (stateless).
  • New features of HTTP1.1 version

    • The default persistent connection saves the amount of communication. As long as any end of the client server does not explicitly propose to disconnect the TCP connection, the connection will always be maintained, and multiple HTTP requests can be sent
    • Pipeline, the client can send multiple HTTP requests at the same time, instead of waiting for the response one by one
    • Principle of Resumable Transmission
  • Features of HTTP2.0 version

    • Binary framing (enclose it with a binary format code)
    • Header compression (HPACK algorithm with special header compression design is set up.)
    • Flow control (some flow control that sets how many bytes of a certain data stream are received)
    • Multiplexing (request and response can be sent at the same time on the basis of sharing TCP link)
    • Request priority (performance can be further optimized by optimizing the interleaving and transmission order of these frames)
    • Server push (that is, the server can send multiple responses to a client request. The server pushes resources to the client without the client's explicit request. (Major update))

What is symmetric encryption and asymmetric encryption

  • Symmetric key encryption refers to the way that the same key is used for encryption and decryption. The biggest problem with this method is the key transmission problem, that is, how to safely send the key to the other party;

  • Asymmetric encryption refers to the use of a pair of asymmetric keys, that is, a public key and a private key. The public key can be released at will, but the private key is only known to you. The party sending the ciphertext uses the other party's public key for encryption, and after the other party receives the encrypted information, it uses its own private key for decryption. Since the asymmetric encryption method does not need to send the private key for decryption, security can be guaranteed; but compared with symmetric encryption, it is very slow

What are cookies and sessions useful for HTTP?

  • The HTTP protocol itself cannot determine the user's identity. So you need cookie or session
What is a cookie
  • A cookie is a file (key-value format) saved by the Web server on the user's browser, which can contain user-related information. When the client initiates a request to the server, the user information in the browser is extracted and sent to the server by http
What is session
  • Session is a piece of storage space allocated by the server to the session during the session between the browser and the server.

  • The server sets the sessionid in the cookie of the client's browser by default. This sessionid corresponds to the cookie. The cookie transmitted by the browser during the request to the server contains the sessionid. The server obtains the information stored in the session according to the sessionid in the transmitted cookie, and then determines The identity information of the session.

The difference between cookie and session
  1. The cookie data is stored on the client, which is less secure, while the session data is stored on the server, which is relatively more secure
  2. The data saved by a single cookie cannot exceed 4K. After the session has no such restricted information, it uses its own private key to decrypt it. Because the asymmetric encryption method does not need to send the private key for decryption, it can ensure security; but compared with symmetric encryption, it is very slow

What are cookies and sessions useful for HTTP?

  • The HTTP protocol itself cannot determine the user's identity. So you need cookie or session
What is a cookie
  • A cookie is a file (key-value format) saved by the Web server on the user's browser, which can contain user-related information. When the client initiates a request to the server, the user information in the browser is extracted and sent to the server by http
What is session
  • Session is a piece of storage space allocated by the server to the session during the session between the browser and the server.

  • The server sets the sessionid in the cookie of the client's browser by default. This sessionid corresponds to the cookie. The cookie transmitted by the browser during the request to the server contains the sessionid. The server obtains the information stored in the session according to the sessionid in the transmitted cookie, and then determines The identity information of the session.

The difference between cookie and session
  1. The cookie data is stored on the client, which is less secure, while the session data is stored on the server, which is relatively more secure
  2. The data saved by a single cookie cannot exceed 4K, and the session has no such limit
  3. The session is stored on the server for a certain period of time. When the number of visits increases and the server performance is occupied, cookies should be used considering server performance.