A basic

1.1 an overview of the

1 What is the difference between the IOS Layer 7 model, TCP/IP protocol family, TCP, and IP? 2 What is the flow of three-way handshake and four-way wave in TCP communication?

1.2 Software Architecture

In life, we often use QQ, wechat, Baidu cloud disk, Goole, IE browser, firefox browser…… . It can be summarized into two broad categories, C/S (client/server) and B/S (browser/server) structures. In these two architectures, two computers communicate over a network using a certain protocol.

1.3 Basic Concepts (Communication Protocol/IP/Port)

  1. Communication protocol

A rule that defines communication between computers on a network. Because computers are made by so many manufacturers, if the data is sent out in different formats, transmitted in different formats, and interpreted in different formats, it is impossible to communicate with all computers.

  1. ip

The unique identification of a computer in the entire network. IP addresses are classified into IPV4 and IPV6. IPV4 occupies 4 bytes and IPV6 occupies 16 bytes. Ipv4 is still in use.

  1. port

In a computer, the unique identification of a process. The port number is expressed in two bytes and ranges from 0 to 65535. The port number from 0 to 1023 is basically the system port. The port number of the program we write should be higher than 1024.

Computer communication stratification

2.1 Are 70% model, TCP/IP protocol family, TCP, IP the same thing?

ISO international standard Telephone organization is studying network communication, established OSI model (Open Systems Interconnect Reference Model). This is the standard 7-tier architecture. (Theoretical hierarchical model)

2 TCP/IP protocol family, first developed by the US Department of Defense ARPA network project, is also DoD model. (Model in practice)

3 TCP, IP These are specific protocols in the network layer.

2.2 Online process of Taobao visit

1 We enter the website of Taobao in the browser.

2 (local) Browser sends the request, application layer – transport layer – Network layer – data link layer

3 (Network transmission process)- > Router – > switch

4 (Taobao server)- “Reach Taobao server -” link layer – “network layer -” Transmission layer – “application layer, obtain data

5 (Response data)- “and then return to the original path.

Three network transport layer parsing

3.1 What is a Socket?

A Socket is an abstraction layer between the application layer and transport layer. Provides a set of interfaces to invoke the TCP/IP protocol API.

3.2 Socket Communication Process

3.3 Details about TCP at the Network Transport Layer

  1. An overview of the

TCP is short for Transmission COntrol Protocol, also known as Transmission COntrol Protocol. It has the following characteristics. A A connection must be established before data transmission and released after data transmission. B Data is transmitted without error, loss, or duplication, and is in the same sequence as the source data. C Data is divided into segments during transmission. D is inefficient because it is a connection-oriented protocol and a connection must be established before communication.

  1. The TCP header is detailed

Source port and destination port: the process from which data comes to the process.

Serial number and confirmation number: key parts of TCP reliable transmission. Ordinal is the ordinal number of the first byte of the data set sent in this paragraph. In a TCP transport stream, each byte has an ordinal number.

URG: Indicates whether the data sent in this paragraph contains urgent data. URG=1: indicates that there is urgent data. The subsequent emergency pointer field is valid only if URG=1.

ACK: indicates whether the preceding confirmation number field is valid. ACK=1: valid. The preceding confirmation number field is valid only if ACK=1. TCP specifies that the ACK value must be 1 after the connection is established.

PSH: Indicates whether the peer party should immediately push the data to the upper layer after receiving the packet. A value of 1 indicates that the peer should immediately submit the data to the upper layer rather than cache it.

RST: This parameter is available only when RST is 1. If you receive a packet with RST=1, your connection to the host is seriously wrong (for example, the host crashes) and you must release the connection and re-establish the connection. Or the last data you sent to the host was faulty and the host refused to respond.

SYN: Used during connection establishment to synchronize serial numbers. When SYN=1, ACK=0, it indicates that this is a packet segment requesting to establish a connection. When SYN=1, ACK=1, the peer agrees to establish a connection. SYN=1, indicating that the packet requests or agrees to establish a connection. The SYN is set to 1 only in the first two handshakes.

FIN: indicates whether the data is sent. If FIN=1, it says, “My data has been sent and you can release the connection.

Window: The sliding window size is used to inform the sender of the cache size of the receiver, so as to control the rate at which the sender sends data and achieve flow control.

Options and Padding: The most common optional field is the Maximum Segment Size (MSS), which is specified by each connector in the first Segment of the communication (the Segment where the SYN flag is set to 1 for connection establishment). This parameter indicates the Maximum Segment length that the local end can accept. The option length does not have to be a multiple of 32 bits, so padding bits are added, that is, adding an extra zero to the field to ensure that the TCP header is a multiple of 32

Data part: The data part of the TCP packet segment is optional. When a connection is established and when a connection is terminated, only the TCP header is exchanged. If one party has no data to send, the header without any data is also used to acknowledge the received data. In many cases of processing timeouts, a segment of the message without any data is also sent.

The principle of TCP disconnection and connection (three-way handshake and four-way wave)

4.1 Field interpretation during data transfer

SYN(Synchronous Establishment of a connection) requests the establishment of a connection and initializes the sequence number in its sequence number field. Set the connection to 1. PSH(Push transmission) prompts the receiving application to immediately read out data from the TCP buffer. FIN(Finish) wants to disconnect the connection. RST(reset) Indicates that the peer party reestablishes the connection. URG(Urgent Urgent) Indicates whether the emergency pointer is valid. Is 2, indicating that one is processed first.

4.2 BIO

4.2.1 BIO code implementation

Public class ServerSocket {public static void main(String[] args) throws Exception { Java.net.ServerSocket serverSocket = new java.net.ServerSocket(8989); Byte [] bytes = new byte[1024]; Try {while (true) {system.out.println (" server blocked, waiting for connection....") ); Socket Accept = serversocket.accept (); Socket accept = serversocket.accept (); InputStream InputStream = ((Socket) Accept).getinputStream (); System.out.println(" Server is blocked, waiting to receive data...." ); int read = inputStream.read(bytes); System.out.println(new String(bytes, 0, read)); Accept.close (); } } catch (Exception e) { e.printStackTrace(); } finally { if (serverSocket ! = null && ! serverSocket.isClosed()) { serverSocket.close(); }}}} // Client code public class ClientSocket {public static void main(String[] args) throws Exception {// Create Socket object, Socket Socket =new Socket("127.0.0.1",8989); OutputStream =socket.getOutputStream(); System.out.println(" client blocked, receiving keyboard input...." ); Scanner Scanner =new Scanner(system.in); String scannerString=scanner.next(); outputStream.write(scannerString.getBytes()); System.out.println(" client entry completed...." ); // Outputstream.write (" itheima-tcp ".getBytes()); // Release resource socket.close(); }}Copy the code

4.2.2 API description in BIO

  1. Socket

A Construction method

Socket() : no-parameter constructor. Socket(InetAddress address,int port) : Creates a stream Socket and connects it to the specified port at the specified IP address. Socket(InetAddress Address,int Port,InetAddress localAddr,int localPort) : Creates a Socket and connects it to the specified remote port at the specified remote address. Socket(String host,int port) : Creates a stream Socket and connects it to the specified port on the specified host. Socket(String host,int Port,InetAddress localAddr,int localPort) : Creates a Socket and connects it to the specified remote port at the specified remote address. The Socket is bound to the supplied local address and port by calling bind().Copy the code

B Common methods

Void bind(SocketAddress bindpoint) : binds a socket to a local address. Void close() : closes the socket. Void Connect (SocketAddress endpoint) : Connects this socket to the server. InetAddress getInetAddress() : Returns the connection address of the socket. InetAddress getLocalAddress() : Gets the local address bound to the socket. InputStream getInputStream() : returns the InputStream for this socket. OutputStream getOutputStream() : returns the OutputStream of this socket. SocketAddress getLocalSocketAddress() : Returns the endpoint address of this socket binding, or null if not yet bound. SocketAddress getRemoteSocketAddress() : Returns the endpoint address of the connection for this socket, or null if no connection has been made. Int getLoacalPort() : Returns the local port bound to this socket. IntgetPort () : Returns the remote port to which this socket is connectedCopy the code
  1. WebSocket

A Construction method

ServerSocket() : no-parameter constructor. ServerSocket(int port) : creates a ServerSocket bound to a specific port. ServerSocket(int Port,int Backlog) : Create server sockets with the specified backlog and bind them to the specified local port. ServerSocket(int Port,int Backlog,InetAddress bindAddr) : Create a server with the specified port, listening backlog, and IP address to be bound locallyCopy the code

B Common methods

Server Accept () : Listens for and receives a connection to this socket. Void bind(SocketAddress endpoint) : binds the ServerSocket to the specified IP address and port number. Void close() : closes the socket. InetAddress getInetAddress() : Returns the local address of this server socket. Int getLocalPort() : Returns the port on which this socket listens. SocketAddress getLocalSoclcetAddress() : Returns the address of the port to which this socket is bound, or null if not yet bound. Int getReceiveBufferSize() : Gets the value of the SO_RCVBUF option for this ServerSocket, which is the recommended buffer size for sockets received from the ServerSocket. The Accept () method returns a Socket object attached to the client Socket object. The Socket's getOutputStream can be used to send information to the client. Enable getIutputStreamke to retrieve data from the client.Copy the code

4.3 TCP Three-way handshake for establishing a connection

  1. The paper

In TCP, a three-way handshake is required to establish a connection. This connection is established by one party actively opening and the other passively opening. Below is a diagram for establishing connections.

  1. The network requests to establish a connection and goes through the three-way handshake process

A The first handshakeOn the first “handshake,” the client sends the SYN flag bit to the server to establish a connection. Seq stands for sequence and number. For example, if the value of Seq is 5, it indicates that data has been sent for 1, 2, 3, and 4 times. In this “handshake”, the value of Seq is 0, indicating that the client did not send data to the server. Len=0 indicates that there is no data to send. The client sends only a SYN flag to the server to indicate a connection.

B The second handshakeOn the second handshake, the server sends the SYN ACK bit to the client. The ACK bit is an acknowledgement of the received packet, indicating that the server has received the connection from the client. The ACK value is 1, indicating that the server expects the sequence number of the next data stream to be sent from the client to be 1, and Seq=0 means that the server did not send data to the client before and did not send data this time, as Len=0 also proves.

C The third handshakeDuring the third handshake, the ACK bit sent by the client to the server is 1 and the Seq value is 1. Seq= L means that this is exactly what the server expects Ack=1. Len=0 indicates that the client did not send data to the server this time, and the client sends ACK bit 1 to the server, indicating that the client expects the Seq value to be 1 next time.

  1. Why three handshakes

In order to prevent the server side to open some useless connections, increase the server overhead. And to prevent an error when an invalid connection request segment is suddenly sent to the server.

4.4 TCP Connection Disconnection (Wave Four Times)

  1. Four times to wave

That is, the release of the TCP connection. The connection must be released actively on one side and passively on the other. The following is a diagram of the client initiating the release of the connection:

Briefly describes the process

A Client to server. I’m shutting it down.

B Server to client, ok, I got it.

C server to client, I turned it off, too.

D client to server. Okay, copy that.

  1. Four waves to execute the flow

A First wave During the first wave, the client sends a FIN ACK flag to the server to inform the server that the client is shut down. Seq=1 indicates that the sequence number of the current data flow is 1. Ack=1 indicates that the client expects the sequence number of the next data flow to be sent by the server to be 1. Len =0: No data is transferred to the server.

B Second wave At the second wave, the server sends an ACK bit to the client. Seq=1 indicates the ACK =1 that the client wants to see. Ack=2 indicates that the server expects the serial number of the next data flow to be sent by the client to be 2. Len =0: No data is transferred to the client.

C Third wave Upon the third wave, the server sends a FIN ACK flag to the client to inform the client that the server is shut down. Seq=1 represents exactly what the client wants to see: Ack=1. Ack=2 indicates that the server expects the serial number of the next data flow to be sent by the client to be 2. Len =0: No data is transferred to the client.

D Fourth Wave Upon the fourth wave, the client sends an ACK bit to the server to inform the server that the client has received the shutdown message. Seq=2 indicates that the server wants to see Ack=2. Ack=2 indicates that the client expects the serial number of the next data stream to be sent by the server to be 2.

The client has not yet sent data to the server. Read is blocked. The new client cannot connect to the server

Five NIO programming

5.1 an overview of the

NIO, also known as non-blocking IO, is a new IO model proposed in JDK1.4

5.2 Component Details

5.2.1 Buffer

  1. An overview of the

According to the physical partition: direct buffer and heap byte buffer. Buffer mode: Write mode and read mode

  1. Buffer implementation Principle

A Three attributes (three attributes of the Buffer) Capacity, position and limit

B write mode



Capacity: The number of elements that can be stored in the array

Position: position where the next element can be inserted. The default value is 0. Each element is moved one bit backwards

Limit: In write mode, limit indicates the first unwritable position (by default, the first unwritable position is worth the next position, which defaults to capacity).

C read mode

Capacity: The number of cells in the array that can store elements.

Position: Buffer changes from write mode to read mode. Position is set to 0. When data is read, position moves forward to the next readable position.

Limit: Indicates the first unreadable position. When the write mode is switched to read mode, limit sets the value of position in write mode. That is, all written data can be read.

5.2.2 Channnl

1 Overview Data transmission is similar to but different from streams. Streams are one-way and mostly single-purpose, either read or write. Channels must be used in conjunction with buffers.

5.2.3 Selector

  1. An overview of the

Each channel has a thread that processes it. In high concurrency, there are many channels and many thread objects are created, increasing memory usage and increasing the time it takes for the CPU to switch between multiple threads. Therefore, high concurrency scenarios are not used.

  1. NIO use channel improvement

The mechanism by which a thread handles multiple task channels is called multiplexing in NIO. After I/O reuse is used, only one thread can process multiple channels, which is advantageous for high-concurrency service scenarios. Add as follows:

The number of threads adjusts dynamically with the number of channels. The core purpose of multiplexing is to operate more channels with a minimum number of threads. The number of threads created depends on the number of channels. One thread is created for every 1023 channels registered

5.2.4 NIO instance

Public class SocketNioServer {public static void main(String[] args) throws Exception {// Define the channel serverketChannel serverSocketChannel = ServerSocketChannel.open(); Bind (new InetSocketAddress("127.0.0.1", 8080)); / / set to non-blocking mode serverSocketChannel. ConfigureBlocking (false); Selector = Selector. Open (); / / registered serverSocketChannel. Register (selector, SelectionKey OP_ACCEPT); While (true) {system.out.println (" waiting for connection, blocking....." ); int count = selector.select(); if (count ! = 0) { Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> Iterator = selectionkeys.iterator (); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); / / the client is connected, has yet to send data if (selectionKey. IsAcceptable ()) {System. Out. Println (" the client is connected, has yet to send data..." ); ServerSocketChannel SSC = (ServerSocketChannel) selectionkey.channel (); SocketChannel socketChannel = ssc.accept(); / / set non-blocking socketChannel. ConfigureBlocking (false); Register the selector socketChannel.register(selector, selectionkey.op_read); Else if (selectionKey.isreadable ()) {system.out.println (" the client successfully sent data "); SocketChannel = (SocketChannel) selectionkey.channel (); ByteBuffer = ByteBuffer. Allocate (1024); int read = socketChannel.read(buffer); While (read > 0) {// Switch the buffer's mode buffer.flip(); System.out.println(new String(buffer.array(), 0, read)); // Clear the buffer buffer.clear(); read = socketChannel.read(buffer); } socketChannel.close(); } iterator.remove(); } } } } }Copy the code

Netty articles

Netty (Netty 1)

How to select codecs in project development? How to solve the PROBLEM of TCP sticky packets? (Netty 2)

Problems and solutions occurred when Netty was used in the subsequent project, to be continued……