OSI Is an open communication system interconnection reference model .OSI The model has 7 The layer structure , Each layer can have several sub layers . OSI Of 7 The layers from top to bottom are 7 application layer 6 The presentation layer 5 The session layer 4 Transport layer 3 The network layer 2 Data link layer 1 The physical layer ; Among them, the top （ namely 7、6、5、4 layer ） Defines the capabilities of the application , below 3 layer （ namely 3、2、1 layer ） Mainly for end-to-end data flow through the network .
application layer ：HTTP
Transport layer ：TCP
The network layer ：IP
The link layer ： The Internet
application layer ： The type of communication this time determines , for instance FTP、DNS、SMTP wait , meanwhile HTTP Protocols also fall into the scope of the application layer . Popular speaking , The application layer decides what to do with this communication .
Transport layer ： The transport layer provides data transfer between two computers , There are two very popular protocols in the transport layer , Namely TCP and UDP agreement . Three handshakes often mentioned in an interview , Four waves are TCP Part of the agreement
The network layer ： The network layer is used to process these flowing packets , That is, if the corresponding packets are routed to the specified location , Select transmission route for network transmission during communication
Data link layer ： The data link layer contains the interface between software and hardware , And the hardware of various network devices , That is, the infrastructure at the bottom of the whole network communication process
TCP The connection is made by three handshakes .
The first handshake
When a client initiates a connection request to the server , The client will send the synchronization sequence label SYN To the server , Here we set up SYN by x, Wait for server to confirm , At this time, the status of the client is SYN_SENT.
The second handshake
When the server receives the client sent SYN after , What the server needs to do is to confirm that the client sent it SYN, Here the server sends a confirmation packet ACK, there ACK by x+1, mean “ I received your message SYN 了 ”, meanwhile , The server will also send a SYN package , Here we set SYN by y. At this time, the status of the server is SYN_RECV.
In a word , Server side send SYN and ACK Two bags .
The third handshake
The client receives... From the server SYN and ACK After package , A confirmation packet needs to be sent to the server ACK,“ I also received your message SYN 了 , I'll send you a confirmation , And then we can fit together ”, there ACK by y+1, After sending , The client and server status is ESTABLISH, namely TCP Successful connection .
In three handshakes , Both the client and server send two packets SYN and ACK, But the two packages on the server side are sent at one time , The two packets of the client are sent in two times .
Disconnect requests can be made by the client , It can also be sent by the server , Here we call A To the end B End request to disconnect .
A To the end B When a client requests to disconnect, it will ask B The client sends a message with FIN Marked segment , there FIN yes Finish It means .
B End receipt A Sent FIN after ,B Now, maybe there is still data to be transmitted , therefore B The end doesn't go to A End send FIN, Instead, send a confirmation number first ACK, mean “ I received your request for disconnection , But I still have data to send out , Just a moment, please ”.
When B The end of the business , So at this time B The end can be disconnected , here B To the end A End send FIN Serial number , It means you can disconnect this time .
A End receipt B Sent by FIN after , Will send to B End send confirmation ACK, And then through two MSL Disconnect after a long time .
MSL yes Maximum Segment Lifetime, Maximum segment lifetime ,2 individual MSL It is the longest time that a message segment is sent and received
service / client ：CLOSED（ In the beginning , Both the server and the client are down ）
Customer ：SYN_SENT（ Send a connection request ）,
service ：LISTEN（ monitor , Waiting for the connection ）,
service ： SYN_RECV（ Upon receipt of the request , reply ）,
service / Customer : ESTABLISHED（ Establishing a connection ）,
Customer ： FIN_WAIT_1（ Disconnect waiting 1）,
service ：CLOSE_WAIT（ Turn off waiting , Transfer the data that has not been transmitted ）,
client ： FIN_WAIT_2（ received ACK, Wait for the data transfer to complete ）,
service ： LAST——ACK（ Send a shutdown request ）,
Customer ： TIME_WAIT（ Reply to disconnect request ）,
Customer ： CLOSING（ Something happened to the client FIN, But I didn't receive the server's ACK, But I received the server's FIN. This happens when the server sends a packet loss , Because sometimes there are accidents in network transmission ）
socket( Socket for short ) It's interprocess communication ⼀ Kind of ⽅ type , It can realize inter process communication between different hosts , We ⽹ All kinds of services are available ⼤ Most of them are based on Socket To complete the communication .
Communication process ：
Communication process ：
udp There is no need to establish a connection between the client and the server , So there is no process of establishing a connection in the program . Before you communicate , Need to set up a network socket .
The server needs to bind a port , Listen for received messages on this port .
The client needs to set up remote IP And port , The information that needs to be delivered needs to be sent to this IP And ports .
For reliable data transmission , TCP The parties to the agreement , You have to maintain a serial number , To identify the packets sent , What has been received by the other party .
The process of three handshakes is that the communication parties inform each other of the starting value of the serial number , And confirm that the other party has received the start value of the serial number .
If it's just two handshakes , At most, only the starting serial number of the connection initiator can be confirmed , The serial number chosen by the other party is not confirmed .
TCP A reliable guarantee of , It's a three way handshake mechanism , This mechanism ensures that the data , Guaranteed his reliability . and UDP There is no the ,udp When the message is sent , Don't verify whether it reaches the other party , So it's not reliable .
The fundamental difference is ： With multiple processes, each process has its own address space (address space), Threads share address space . All the other differences come from this ：
Advantages of multithreading ：
The limitations of multithreading ：
Large scale connectivity , First, exclude multiprocessing implementations , Large scale connections using multiple processes require a lot of memory space
Consider using a multithreaded server or this coprocessor to implement a concurrent server .
Coprocessor to achieve server advantages :
1. No thread context switching overhead
2. There is no need for atomic locking and synchronization
3. Easy to switch control flow , Simplify the programming model
4. High concurrency + High scalability + Low cost ： One CPU Supporting tens of thousands of projects is not a problem . So it is very suitable for high concurrency processing .
1. Can't use multi-core resources ： The essence of a process is a single thread , It cannot at the same time Single CPU For multiple cores , The process needs to work with the process to run in multiple CPU On . Of course, most of the applications we write in our daily life don't have to , Unless it is cpu Intensive application .
2. To block （Blocking） operation （ Such as IO when ） It will block the whole program
analysis ：TCP/IP The transport layer of the agreement is TCP and UDP Two agreements .
analysis ： Convert a domain name into something that the network can recognize ip Address , And then the actual network communication .
analysis ： The connectionless system is UDP Service delivery , Just throw out the byte stream
analysis ：IP The address is expressed in dotted decimal system
analysis ：OSI The data units exchanged between layers are called service data units SDU.
analysis ：TCP It's a unicast two-way service
analysis ：TCP The protocol uses three handshakes to connect
analysis ： The so-called port , It's like a house number , The client can use the ip Address to find the corresponding server , But there are many ports on the server side , Each application corresponds to a port number .
analysis ： Fight for the TCP The handshake process is ：SYN,SYN+ACK,ACK
analysis ：OSI The application layer is from the top to the bottom of the seven layers , The presentation layer , The session layer , Transport layer , The network layer , Data link layer , The physical layer .
analysis ：ACK It should be transmitted from SYN+1 namely ACK=x+1,SYN unchanged ,Seq by y
analysis ： The network is classified as follows
analysis ：RARP agreement : The physical address can be converted to IP Address
ARP agreement : Can be IP Address translation to physical address
analysis ：Netstat: Shows which network connections are working . Default display TCP Connected information .
ping: Check network connectivity , It can help us analyze and judge the network fault
Ipconfig: Show IP Address 、 Subnet mask and default gateway values .
Nslookup: Inquire about DNS The record of , Check whether domain name resolution is normal , It is used to diagnose network problems when the network fails .
analysis ： The main elements are generally understood : How to say it , What to say , When to talk