1. Understanding socket programming, what is its protocol?

A socket, usually called a socket, describes an IP address and port and is a handle to a communication chain. Applications make and respond to network requests through sockets.

The server and client interact through sockets. The server needs to be bound to a local port number, and the client needs to declare which port it connects to at which address, so that the server and client can connect.

The connection process between sockets can be divided into three steps, depending on how the connection is initiated and to which destination the local socket is connecting: server listening, client request, and connection confirmation. (1) Server monitoring: the server socket does not locate the specific client socket, but is in the state of waiting for connection and monitors the network status in real time. (2) Client request: it refers to the connection request made by the client socket, and the target to be connected is the server socket. To do this, the client-side socket must first describe the socket of the server to which it is connecting, specifying the address and port number of the server-side socket, and then make a connection request to the server-side socket. (3) Connection confirmation: when the server socket listens to or receives the connection request of the client socket, it responds to the request of the client socket, establishes a new thread, and sends the description of the server socket to the client. Once the client confirms the description, the connection is established. The server socket continues to listen, receiving connection requests from other client sockets. Socket encapsulates and applies TCP/IP. In TCP/IP, TCP establishes a reliable connection through a three-way handshake. First handshake: The client attempts to connect to the server and sends a SYN packet (syn Sequence number Synchronize Sequence Numbers) to the server. Syn =j then the client enters the SYN_SEND state and waits for confirmation from the server. Second handshake: The server receives and acknowledges a SYN packet (ACK = J +1) from the client and sends a SYN packet (ACK = K) to the client. At this time, the server enters the SYN_RECV state. Third handshake: After receiving the 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 enter the ESTABLISHED state to complete the three-way handshake.

The connection between the server socket and the client socket is called the “three-way handshake”.

  1. The difference between socket and HTTP

Socket connection: A socket is not a protocol. It is an API that encapsulates TCP/IP. Realize the physical connection between the server and the client, and data transmission. Sockets are at the transport layer of network protocols, including TCP and UDP. The socket connection is a long connection. Theoretically, once the client and server establish a connection, they will not actively break the connection. However, the connection may be disconnected due to various environmental factors, such as server or client host breakdown, network failure, or no data transmission between the two hosts for a long time. The network firewall may disconnect the connection to release network resources. Therefore, when there is no data transfer in a socket connection, heartbeat messages need to be sent to maintain the connection. The data transmitted by the socket can be customized and is byte level. The data volume is small, can be encrypted, and the data security is high. It is suitable for real-time information exchange between the Client and Server.

HTTP connection: HTTP is an application layer protocol based on TCP/IP and defines the specifications for data transmission. HTTP is request-response based and is a short connection, that is, the client sends a request to the server, the server responds and the connection is broken. HTTP is a stateless protocol. For its stateless characteristics, it needs a stateful form in practical applications. Therefore, session/cookie technology is generally used to solve this problem. HTTP transmission speed is slow, data packets are large, data transmission security is poor, real interaction, server performance pressure.

  1. What are the application scenarios of socket and HTTP

Socket is generally used to compare real-time communication and high real-time situation, such as push, chat, maintain heartbeat long connection, etc. HTTP is generally used for situations where real-time requirements are not so high, such as information feedback, image uploading, and obtaining news information.

The reason for not using sockets in some cases is that once a socket is connected, it stays connected, which causes blocking IO, as opposed to non-blocking IO. Blocking IO means that the client requests the server, and the server processes the request and returns the value to the client. However, when the client side too many concurrent requests, the server side can not handle, will be jammed, so put forward a non-blocking IO, the client side requests the server side, the server side has some listeners responsible for receiving the request, accepted, told the client has accepted, after processing the data sent to the client side.

Blocking and non-blocking are concerned with the state of the program while it waits for the result of the call (message, return value). A blocking call means that the current thread is suspended until the result of the call is returned. The calling thread does not return until it gets the result. A non-blocking call does not block the current thread until the result is not immediately available.