My Github address
Notes on data Structures and Algorithms
Notes for geek Time iOS Developer Class
IOS large factory interview high frequency algorithm summary
Summary of iOS interview materials
Serial number and confirmation number
- The complete process of client and server from establishing connection to sending data is shown below
- The first step is to establish a connection, and the client sends a message to the server
SYN
, the data part is0
, only header information. - The sender:
seq = 0
.len = 0
- Step 2: Receiver:
ack = 0 + 1
- Step three, the sender in response to the previous
ack
, the client responds to the server,seq
for1
. The sender expects the next response from1
Start, soack = 1
- Step three and step four are the client sending information to the server, so
seq
andack
Same, except that the data part of step 4 is not0
- Steps 5 through 8 are for the server to send information to the client, so
ack
Are the length of data received last timek + 1
. - The difference is that each request
seq
Is the previous length plus the current data length. - Step 9 Client
ack
Is the sum of previous server data+ 1
And theseq
Indicates the length of the last sent data+ 1
- Summarized below
Establish a connection
Three-way handshake
CLOSED
:client
In the closed stateLISTEN
:server
In listening state, waitingclient
The connectionSYN-RCVD
Said:server
To accept theSYN
Packet when receivedclient
theACK
After the message, it will enterESTABLISHED
stateSYN-SENT
Said:client
Has been sentSYN
Message, waitserver
The second handshakeESTABLISHED
: Indicates that the connection is established
Characteristics of the first two handshakes
SYN
All set to1
- The length of the data part is
0
TCP
The length of the head is generally32
byte- Fixed head:
20
byte - Options:
12
byte
- Fixed head:
- The two sides will exchange and confirm some information
- Such as
MSS
, supported or notSACK
.Window scale
(Window zoom coefficient), etc - All of this data is in there
TCP
In the options section of the header (12
Bytes)
- Such as
doubt
- Why do you want to make a connection
3
Time to shake hands,2
Not twice?- Main purpose: prevention
server
End keeps waiting, wasting resources
- Main purpose: prevention
- If you set up a connection you just need to
2
Second handshake, what might happen- Assuming that
client
The first connection request segment that is sent arrives some time after the connection is released because of network latencyserver
- It was supposed to be an expired request, but
server
This invalid request was mistakenly receivedclient
A new connection request made again - so
server
As to theclient
Sends a confirmation message, agreeing to establish a connection - If not adopted”
3
Second handshake “, then as long asserver
An acknowledgement is issued and a new connection is established - Because now
client
There is no real desire to connect to the server, so it is ignoredserver
Will not confirm toserver
To send data - But in the
server
Thinking a new connection has been made and waitingclient
Send in the data, and so,server
A lot of resources are wasted
- Assuming that
- This can be prevented by the three-way handshake
- In the case above,
client
There is noserver
To send confirmation,server
Since I can’t get confirmation, I knowclient
There is no requirement to establish a connection
- In the case above,
- What happens if the third handshake fails?
- At this time
server
The status ofSYN-RCVD
If can’t waitclient
theACK
.server
It will resendSYN+ACK
package - if
server
Many times to resendSYN+ACK
Are such as less thanclient
theACK
, will be sentRST
Package to force the connection to close
- At this time
Release the connection
State interpretation
FIN-WAIT-1
: indicates that you want to close the connection- Sent to each other
FIN
The packet is then enteredFIN-WAIT-1
state
- Sent to each other
CLOSE-WAIT
: Indicates that the system is waiting to close- When the other party sends
FIN
Give it to yourself, and you’ll respond with oneACK
The packet is sent to the peerCLOSE-WAIT
state - In this state, you need to consider whether you still have data to send to the other party, if not, send
FIN
Message to the Other party
- When the other party sends
FIN-WAIT-2
: As long as the other party sendsACK
Once confirmed, the active party will be inFIN-WAIT-2
Status and wait for the sender to send itFIN
messageCLOSING
: a rare exception- Means that you send
FIN
The packet is not received from the peer. ProcedureACK
Message, but also received the other partyFIN
message - If both parties are about to close the connection at about the same time, then both parties are sending simultaneously
FIN
In the case of a packet, that is, it appearsCLOSING
state - Indicates that both parties are closing the connection
- Means that you send
LAST-ACK
: Passive closed party in sendingFIN
The packet is the last one to wait for the peerACK
message- When I received
ACK
After receiving the packet, you can enterCLOSED
State of the
- When I received
TIME-WAIT
: Indicates that the packet is receivedFIN
The message was sentACK
Message, and so on2MSL
Then you can enterCLOSED
State of the- if
FIN-WAIT-1
State, received the other side at the same timeFIN
Logo andACK
Flag for a packet- I can go right into
TIME-WAIT
State without having to go throughFIN-WAIT-2
state
- I can go right into
- if
CLOSED
: Closed state- Some states are difficult to use because of their short duration
netstat
Command to see, for exampleSYN-RCVD
,FIN-WAIT-1
Etc.
details
TCP/IP
The protocol stack is designed to allow either party to initiate a disconnect request first. So what I’m showing here isclient
Initiate disconnectionclient
sendACK
After, need to have aTIME-WAIT
Phase, wait some time before actually closing the connection- It’s usually waiting
2 times
MSL(Maximum Segment Lifetime) ‘Maximum Segment LifetimeMSL
isTCP
A message inInternet
Maximum survival time on- Each specific
TCP
The implementation must choose a definiteMSL
Value,RFC 1122
Advice is2
minutes - Prevents packets generated in this connection from being mistransmitted to the next connection (because all packets in this connection will be present)
2MSL
Disappeared in time)
- It’s usually waiting
- if
client
sendACK
And then he released it, and then because of the Internet,server
Have not receivedclient
theACK
.server
Will resendFIN
- Here’s what might happen:
client
If there is no response, the server will wait, or even resend many timesFIN
Waste of resourcesclient
A new application just assigned the same port number, and the new application receivedFIN
Immediately after the disconnect operation, it may have wanted to connectserver
jointed
- Here’s what might happen:
doubt
- Why do I release connections when I do
4
Time to wave?TCP
The mode is full duplex- The first
1
Second wave: when the host1
aFIN
When the message segment- The host
1
Tell the host2
, the host1
There is no more data to send, however, at this point the host1
Still acceptable from the host2
The data of
- The host
- The first
2
Second wave: when the host2
returnACK
When the message segment- The host
2
The host is known1
There is no data to send, but host 2 can still send data to the host1
the
- The host
- The first
3
Second wave: when the host2
Also send theFIN
When the message segment- The host
2
Tell the host1
, the host2
There’s no more data to send
- The host
- The first
4
Second wave: when the host1
returnACK
When the message segment- The host
1
The host is known2
No more data to send. Then officially disconnect the wholeTCP
The connection
- The host