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 serverSYN, 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 previousack, the client responds to the server,seqfor1. The sender expects the next response from1Start, soack = 1

  • Step three and step four are the client sending information to the server, soseqandackSame, except that the data part of step 4 is not0

  • Steps 5 through 8 are for the server to send information to the client, soackAre the length of data received last timek + 1.
  • The difference is that each requestseqIs the previous length plus the current data length.
  • Step 9 ClientackIs the sum of previous server data+ 1And theseqIndicates the length of the last sent data+ 1

  • Summarized below

Establish a connection

Three-way handshake

  • CLOSED:clientIn the closed state
  • LISTEN:serverIn listening state, waitingclientThe connection
  • SYN-RCVDSaid:serverTo accept theSYNPacket when receivedclienttheACKAfter the message, it will enterESTABLISHEDstate
  • SYN-SENTSaid:clientHas been sentSYNMessage, waitserverThe second handshake
  • ESTABLISHED: Indicates that the connection is established

Characteristics of the first two handshakes

  • SYNAll set to1
  • The length of the data part is0
  • TCPThe length of the head is generally32byte
    • Fixed head:20byte
    • Options:12byte
  • The two sides will exchange and confirm some information
    • Such asMSS, supported or notSACK.Window scale(Window zoom coefficient), etc
    • All of this data is in thereTCPIn the options section of the header (12Bytes)

doubt

  • Why do you want to make a connection3Time to shake hands,2Not twice?
    • Main purpose: preventionserverEnd keeps waiting, wasting resources
  • If you set up a connection you just need to2Second handshake, what might happen
    • Assuming thatclientThe 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, butserverThis invalid request was mistakenly receivedclientA new connection request made again
    • soserverAs to theclientSends a confirmation message, agreeing to establish a connection
    • If not adopted”3Second handshake “, then as long asserverAn acknowledgement is issued and a new connection is established
    • Because nowclientThere is no real desire to connect to the server, so it is ignoredserverWill not confirm toserverTo send data
    • But in theserverThinking a new connection has been made and waitingclientSend in the data, and so,serverA lot of resources are wasted
  • This can be prevented by the three-way handshake
    • In the case above,clientThere is noserverTo send confirmation,serverSince I can’t get confirmation, I knowclientThere is no requirement to establish a connection
  • What happens if the third handshake fails?
    • At this timeserverThe status ofSYN-RCVDIf can’t waitclienttheACK.serverIt will resendSYN+ACKpackage
    • ifserverMany times to resendSYN+ACKAre such as less thanclienttheACK, will be sentRSTPackage to force the connection to close

Release the connection

State interpretation

  • FIN-WAIT-1: indicates that you want to close the connection
    • Sent to each otherFINThe packet is then enteredFIN-WAIT-1state
  • CLOSE-WAIT: Indicates that the system is waiting to close
    • When the other party sendsFINGive it to yourself, and you’ll respond with oneACKThe packet is sent to the peerCLOSE-WAITstate
    • In this state, you need to consider whether you still have data to send to the other party, if not, sendFINMessage to the Other party
  • FIN-WAIT-2: As long as the other party sendsACKOnce confirmed, the active party will be inFIN-WAIT-2Status and wait for the sender to send itFINmessage
  • CLOSING: a rare exception
    • Means that you sendFINThe packet is not received from the peer. ProcedureACKMessage, but also received the other partyFINmessage
    • If both parties are about to close the connection at about the same time, then both parties are sending simultaneouslyFINIn the case of a packet, that is, it appearsCLOSINGstate
    • Indicates that both parties are closing the connection
  • LAST-ACK: Passive closed party in sendingFINThe packet is the last one to wait for the peerACKmessage
    • When I receivedACKAfter receiving the packet, you can enterCLOSEDState of the
  • TIME-WAIT: Indicates that the packet is receivedFINThe message was sentACKMessage, and so on2MSLThen you can enterCLOSEDState of the
    • ifFIN-WAIT-1State, received the other side at the same timeFINLogo andACKFlag for a packet
      • I can go right intoTIME-WAITState without having to go throughFIN-WAIT-2state
  • CLOSED: Closed state
  • Some states are difficult to use because of their short durationnetstatCommand to see, for exampleSYN-RCVD,FIN-WAIT-1Etc.

details

  • TCP/IPThe protocol stack is designed to allow either party to initiate a disconnect request first. So what I’m showing here isclientInitiate disconnection
  • clientsendACKAfter, need to have aTIME-WAITPhase, wait some time before actually closing the connection
    • It’s usually waiting2 timesMSL(Maximum Segment Lifetime) ‘Maximum Segment Lifetime
      • MSLisTCPA message inInternetMaximum survival time on
      • Each specificTCPThe implementation must choose a definiteMSLValue,RFC 1122Advice is2minutes
      • Prevents packets generated in this connection from being mistransmitted to the next connection (because all packets in this connection will be present)2MSLDisappeared in time)
  • ifclientsendACKAnd then he released it, and then because of the Internet,serverHave not receivedclienttheACK.serverWill resendFIN
    • Here’s what might happen:
      • clientIf there is no response, the server will wait, or even resend many timesFINWaste of resources
      • clientA new application just assigned the same port number, and the new application receivedFINImmediately after the disconnect operation, it may have wanted to connectserverjointed

doubt

  • Why do I release connections when I do4Time to wave?
    • TCPThe mode is full duplex
    • The first1Second wave: when the host1aFINWhen the message segment
      • The host1Tell the host2, the host1There is no more data to send, however, at this point the host1Still acceptable from the host2The data of
    • The first2Second wave: when the host2returnACKWhen the message segment
      • The host2The host is known1There is no data to send, but host 2 can still send data to the host1the
    • The first3Second wave: when the host2Also send theFINWhen the message segment
      • The host2Tell the host1, the host2There’s no more data to send
    • The first4Second wave: when the host1returnACKWhen the message segment
      • The host1The host is known2No more data to send. Then officially disconnect the wholeTCPThe connection