33. Nio (selector (handling message boundary issues (capacity exceeded)))

Nio (selector (handle message boundary issues (capacity exceeded)))

Step 1: We copy the previous problem of dealing with sticky packets to deal with this buffer (before we dealt with the buffer, I copied the data it read directly into the buffer)

 

 

Client case: (read here is a blocking method, so we don’t need to break the point before, and then debug runs)

 

Step 2: The case becomes beyond the buffer size

 

Test: (The logic is that when our server does not finish reading for the first time (not the last one is not complete), it will read the second time, which is why it will print the second time as complete information)

 

Optimization idea: (expansion, when it is found that the buffer is not enough, it will be doubled)

! ! ! Note that since we need to use the same buffer here. Then buffer can no longer be set as a local variable

The first step is to read the first part and find that the buffer is not enough, so the buffer should be expanded. Then copy the content of the first part (expanded buffer) and read our second part to the end of the first part

 

Guess you like

Origin blog.csdn.net/logtcm4/article/details/127821051