Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Java Engineer Computer Network (IV) interview question (day31)

Duan Xuechang 2022-05-14 15:58:46 阅读数:1 评论数:0 点赞数:0 收藏数:0

1 Introduce to you HTTPS The process of .
Reference answer
HTTPS There are three keys involved in the transmission process : Public and private keys on the server side , For asymmetric encryption ; Client generated random key , For symmetric encryption . One HTTPS The request actually contains two HTTP transmission , As shown in the figure below, it can be subdivided into the following 8 Step :
 Insert picture description here
The client initiates... To the server HTTPS request , Connected to the server 443 port
The server side has a key pair , Public key and private key , It is used for asymmetric encryption , The server holds the private key , Don't let it out , The public key can be sent to anyone .
The server sends its own public key to the client .
After the client receives the server's public key , The public key will be checked , Verify its validity , If you find a problem with the public key , that HTTPS Transmission can't continue . Strictly speaking , This should be to verify the validity of the digital certificate sent by the server , About how clients verify the validity of digital certificates , It will be explained below . If the public key is qualified , Then the client will generate a random value , This random value is the key used for symmetric encryption , We call this key client key, That is, the client key , In this way, it is easy to distinguish conceptually from the server-side key . Then use the public key of the server to encrypt the client key asymmetrically , In this way, the client key becomes ciphertext , thus ,HTTPS For the first time HTTP End of request .
The client will initiate HTTPS Second of HTTP request , Send the encrypted client key to the server .
After the server receives the ciphertext from the client , Will use their own private key for asymmetric decryption , The decrypted plaintext is the client key , Then use client key to encrypt data symmetrically , So the data becomes ciphertext .
Then the server sends the encrypted ciphertext to the client .
The client receives the ciphertext sent by the server , Use client key to decrypt it symmetrically , Get the data sent by the server . such HTTPS Second of HTTP End of request , Whole HTTPS Transmission complete .

2 Introduce to you HTTP Your failure code .
Reference answer
HTTP The error code of contains client error 4XX And server errors 5XX , The two errors are as follows :
Client error 4XX
This kind of status code is applicable when the client seems to have an error . In addition to responding to HEAD Out of request , The server should contain an entity that includes a description of the error condition , And whether it is temporary or permanent . These status codes apply to any request method . The user agent should present all contained entities to the user .
If the client is sending data , Use TCP Your server should be when the server closes the output link , Carefully ensure that the Client acknowledges receipt of the packet containing the response (receipt of the packet(s) ) . If the client continues to send data after the server is shut down , Server's TCP A reset package will occur on the stack to the client , It could be in HTTP The application reads and interprets the client's unacknowledged input buffer (input buffers) Erase it before .
400( Wrong request ) The server does not understand the syntax of the request .
401( unauthorized ) Request for authentication . After logging in , The server may return this response to the page .
403( It is forbidden to ) Server rejects request . If in Googlebot Show this status code when trying to grab a valid page on your site ( You can go to Google See this status code on the network capture page under diagnosis in Webmaster tool ), that , This could be a denial by your server or host Googlebot Visit it .
404( Not found ) The server could not find the requested page . for example , If the request is for a web page that does not exist on the server , that , The server usually returns this code .
If you don't have robots.txt file , And you are Google Webmaster Tools ” The diagnosis ” Labeled robots.txt This state was found on the page , that , This is the right state . However , If you have robots.txt The document found this state again , that , This means your robots.txt The file may be named incorrectly or in the wrong location .( The file should be on the top-level domain name , And it should be called robots.txt).
If you are in Googlebot This status is found on the URL you are trying to crawl ( be located ” The diagnosis ” Labeled HTTP On the error page ), that , This means Googlebot What you're tracking may be an invalid link in another page ( Old links or links with incorrect input ).
405( Method disable ) Disable the method specified in the request .
406( Don't accept ) Unable to use the requested content feature to respond to the requested page .
407( Proxy authorization required ) This status code and 401( unauthorized ) similar , But it specifies that the requester should use a proxy to authorize . If the server returns this response , that , The server also indicates the proxy that the requester should use .
408( request timeout ) The server timed out waiting for a request .
409( Conflict ) Server conflicts when completing request . The server must contain information about conflicts in the response . The server is responding to a conflict with the previous request PUT This code may be returned on request , A list of the differences between the two requests is also provided .
410( deleted ) If the requested resource has been permanently deleted , that , The server will return this response . The code is associated with 404( Not found ) Code similarity , But in the case of resources that existed before but no longer exist , Sometimes it can replace 404 Code appears . If the resource has been permanently deleted , that , You should use 301 The code specifies the new location of the resource .
411( Effective length required ) The server will not accept requests with invalid content length header fields .
412( Preconditions not met ) The server does not meet one of the prerequisites set by the requester in the request .
413( Request entity too large ) The server was unable to process the request , Because the request entity is too large , The processing power of the server has been exceeded .
414( Requested URI Too long ) Requested URI( Usually web address ) Too long , The server is unable to process .
415( Unsupported media types ) The requested format is not supported by the requested page .
416( The request scope does not meet the requirements ) If the request is for an invalid range of pages , that , The server will return this status code .
417( Not meeting expectations ) Server not satisfied ” expect ” Request header field requirements .
Server error 5XX
The response status code is numeric 5 start , It indicates that this kind of server knows its error or cannot execute the request . There was a response HEAD Out of request , The server should include an entity that contains a description of the error condition , And whether he is temporary or permanent , The user agent should present all contained entities to the user . These response codes apply to any request method .
500( Server internal error ) Server encountered an error , Unable to complete request .
501( Not yet implemented ) The server does not have the function to complete the request . for example , When the server does not recognize the request method , The server may return this code .
502( Bad Gateway ) Server as gateway or proxy , Invalid response received from upstream server .
503( Service not available ) The server is not available at this time ( Due to overload or shutdown maintenance ). Usually , It's just a temporary state .
504( gateway timeout ) Server as gateway or proxy , Failed to receive request from upstream server in time .
505(HTTP Version not supported ) The server does not support HTTP Protocol version .

3 Tell me what you know http Status code .
Reference answer
HTTP The status code consists of three decimal digits , The first decimal number defines the type of status code , The last two numbers have no role in classification .HTTP Status codes are divided into 5 Types , Classification and classification description are shown in the table below :
 Insert picture description here

The common status codes of each category are as follows :
2xx (3 Kind of )
200 OK: Indicates that the request sent from the client to the server is processed normally and returned ;
204 No Content: Indicates that the request sent by the client to the client has been successfully processed , But the main body of the entity is not included in the returned response message ( No resources to return );
206 Patial Content: Indicates that the client has made a range request , And the server successfully executed this part of GET request , The response message contains the message from Content-Range Specify the scope of entity content .
3xx (5 Kind of )
301 Moved Permanently: Permanent redirection , Indicates that the requested resource is assigned a new URL, After that, the changed URL;
302 Found: Temporary redirections , Indicates that the requested resource is assigned a new URL, I hope to use the new URL;
301 And 302 The difference between : The former is permanent movement , The latter is a temporary move ( It may change later URL)
303 See Other: Indicates that the requested resource is assigned a new URL, You should use GET Method to get the requested resource ;
302 And 303 The difference between : The latter makes it clear that the client should adopt GET How to get resources
304 Not Modified: Indicates that the client sends a condition ( Means to adopt GET Method contains if-Match、If-Modified-Since、If-None-Match、If-Range、If-Unmodified-Since Any of the first ) The request of , The server side allows access to resources , However, if the request meets the conditions, the status code is returned ;
307 Temporary Redirect: Temporary redirection , And 303 It has the same meaning ,307 Will follow browser standards will not be from POST become GET;( Different browsers may have different situations );
4xx (4 Kind of )
400 Bad Request: Indicates that there is a syntax error in the request message ;
401 Unauthorized: Unauthorized , Need to pass through HTTP authentication ;
403 Forbidden: The server denied the access ( There is a problem with access rights )
404 Not Found: Indicates that the requested resource could not be found on the server , besides , It can also be used when the server rejects the request but does not want to give the reason for rejection ;
5xx (2 Kind of )
500 Inter Server Error: Indicates that the server made an error while executing the request , It could be web Applications exist bug Or some temporary mistake ;
503 Server Unavailable: Indicates that the server is temporarily overloaded or under shutdown maintenance , Unable to process request ;

4 301 and 302 What's the difference? ?
Reference answer
301 and 302 The difference is that ,301 Redirection is permanent , The search engine grabs the new content and exchanges the old URL with the redirected one .302 Redirection is temporary redirection , Search engines will grab new content and save old URLs . As the server goes to 302 Code , Search engines think the new web address is only temporary .

5 302 and 304 What's the difference? ?
Reference answer
302 and 304 Are two different response status codes for web page requests .302 ( Temporary movement ) Express The server is currently responding to requests from pages in different locations , But the requester should continue to use the original location for future requests . 304 ( not changed ) Express Since last request , The requested page has not been modified . When the server returns this response , Page content will not be returned .

6 Please describe a complete HTTP Process of request .
Reference answer
 Insert picture description here
DNS Analysis flow chart
First of all, the client location is a computer or mobile phone , After opening the browser , Such as input http://www.zdns.cn Domain name of , It starts with a browser DNS Resolve request , If no results are found in the local cache server , First, it will query the root server , The root server records the location of each top-level domain's server , When requesting from the root server http://www.zdns.cn When , The root server will return .cn Location information of the server ;
The recursive server gets .cn After the authoritative server address , Will ask .cn The authoritative server , Do you know http://www.zdns.cn The location of . This is the time .cn Authoritative server looks up and returns http://zdns.cn The address of the server ;
Go on to http://zdns.cn The authoritative server to query this address , from http://zdns.cn The server of gives the address :202.173.11.10;
End up in http Link to , Visit the website smoothly ;
Additional explanation : Once the recursive server gets the parsing record , It will cache locally , If the next time the client requests the same domain name from the local recursive domain name server , It's not going to be checked one by one , Because there is already cache in the local server , This is the time to put http://www.zdns.cn Just return the record to the client .

7 What is redirection ?
Reference answer
Redirect (Redirect) It is to redirect various network requests to other locations through various methods ( Such as : Page redirection 、 Domain redirection 、 The change of route selection is also a kind of redirection of data message through the route ).
Analysis of the answer
When redirection is required
(1) Website adjustment ( Such as changing the structure of the web directory );
(2) The page was moved to a new address ;
(3) Page extension changed ( If the application needs .php Change to .Html or .shtml).
In these cases , If you don't redirect , Then the old address in the user's favorite or search engine database can only let the visiting customer get one 404 Page error message , Loss of traffic ; Moreover, some websites that register multiple domain names , It is also necessary to redirect users accessing these domain names to the main site automatically .
Common redirection methods
(1)301 redirect----- Permanent transfer
When a user or search engine makes a browsing request to the web server , Server returned HTTP Header information in data stream (header) One of the status codes in , Indicates that this page is permanently transferred to another address .
(2)302 redirect----- A temporary transfer (Temporarily Moved )
Also known as temporary redirection (temporary redirect), A command to the web browser to display the different... That the browser is required to display URL, When a web page has experienced short-term URL Use when changing . A temporary redirection is a server-side redirection , Can be correctly handled by search engine spiders .
The difference between old and new redirection methods
302 Redirection is temporary redirection , Search engines will grab new content and save old URLs . As the server goes to 302 Code , Search engines think the new web address is only temporary ;
301 Redirection is permanent , The search engine grabs the new content and exchanges the old URL with the redirected one .
Why? 302 Redirection is associated with URL hijacking
From website A Make one 302 Redirect to web address B when , The implied meaning of the host server is the web address A It's possible to change your mind at any time , Redisplay your own content or turn to another place . Most search engines in most cases , When I received 302 When redirecting , Generally, just grab the target website , In other words, the website B. If search engines encounter 302 When turning , 100% of the target URL is captured B Words , Don't worry about the website URL Hijacked . The problem is , Sometimes search engines , In especial Google, You can't always grab the target URL .
for instance , sometimes A The website is very short , But it makes a 302 Redirect to B website , and B The website is a long, messy URL website , There may even be some parameters such as question marks . Very natural ,A The website is more user-friendly , and B The website is ugly , Not user friendly . At this time Google It is likely that the URL will still be displayed A. Because the search engine ranking algorithm is just a program, not a person , In case of 302 When redirecting , You can't accurately determine which website is more appropriate like people , This creates a web site URL The possibility of hijacking . in other words , An immoral person on his own website A Make one 302 Redirect to your web address B, For some reason , Google The search results still show the web address A, But the content of the web page is your website B Content on , This situation is called web address URL hijacked . What you worked hard to write was stolen by others .
302 URL caused by redirection URL Hijacking phenomenon , It's been around for a while . But so far , There seems to be no better solution . In the ongoing data center transformation ,302 Redirection is also one of the goals to be solved . From some search results , Website hijacking has improved , But it's not completely solved .

8 What's the difference between redirection and request forwarding ?
Reference answer
Request forwarding
The client first sends a request to the server , Server side found matching servlet, And assign it to execute , When this servlet After execution , It calls getRequestDispacther() Method , Forward the request to the specified student_list.jsp, The whole process is done on the server side , And it's done in the same request , therefore servlet and jsp It's the same request, stay servlet Everything in it , stay student_list All of them can be taken out , therefore ,student_list Can put the result getAttribute() come out ,getAttribute() After the execution, return the result to the client . The whole process is a request , A response .
Redirect
The client sends a request to the server , Server matching servlet,servlet After processing, it was called sendRedirect() Method , Return this response to the client immediately , The response line tells the client that you have to send another request , To visit student_list.jsp, Then the client receives the request , Make a new request at once , To request student_list.jsp, Here two requests do not interfere with each other , Are independent of each other , in front request Inside setAttribute() Anything of , In the rear request There's nothing in it . so , stay sendRedirect() There are two requests , Two responses .( The server sends a... To the browser 302 Status code and a location The message header , After receiving the request, the browser will send a request again according to the redirection address )
Difference between the two
(1) Number of requests : Redirection is when the browser sends a request to the server and receives a response, and then sends a request to a new address again , Forwarding is when the server receives a request and jumps to a new address in order to complete the response ; Redirection has been requested at least twice , Forward the request once ;
(2) The address bar is different : The redirection address bar will change , The forwarding address bar will not change ;
(3) Whether to share data : Redirect two requests without sharing data , Forward a request to share data ( stay request Level use information sharing , Using redirection is bound to go wrong );
(4) Jump restrictions : Redirection can jump to any URL, Forwarding can only jump to this site resource ;
(5) The behavior is different : Redirection is client behavior , Forwarding is server-side behavior .

9 Introduce to you DNS The process of addressing .
Reference answer
 Insert picture description here DNS Analysis flow chart
First of all, the client location is a computer or mobile phone , After opening the browser , Such as input http://www.zdns.cn Domain name of , It starts with a browser DNS Resolve request , If no results are found in the local cache server , First, it will query the root server , The root server records the location of each top-level domain's server , When requesting from the root server http://www.zdns.cn When , The root server will return .cn Location information of the server ;
The recursive server gets .cn After the authoritative server address , Will ask .cn The authoritative server , Do you know http://www.zdns.cn The location of . This is the time .cn Authoritative server looks up and returns http://zdns.cn The address of the server ;
Go on to http://zdns.cn The authoritative server to query this address , from http://zdns.cn The server of gives the address :202.173.11.10;
End up in http Link to , Visit the website smoothly ;
Additional explanation : Once the recursive server gets the parsing record , It will cache locally , If the next time the client requests the same domain name from the local recursive domain name server , It's not going to be checked one by one , Because there is already cache in the local server , This is the time to put http://www.zdns.cn Just return the record to the client .
Analysis of the answer
What is? DNS
DNS It's the domain name system , It's a core service in the Internet , Is used to implement domain names and IP A distributed database with address mapping , It can make it easier for users to access the Internet , Instead of remembering what can be read directly by the machine IP Number string . By hostname , Get the... Corresponding to the host name IP The process of address is called domain name resolution ( Or hostname resolution ).
Domain name resolution structure
 Insert picture description here
As shown in the figure above , Domain name structure is tree structure , The top of the tree represents the root server , The next layer of roots is known as .com、.net、.cn And so on .cn、.uk And so on , It's called the top level domain . The domain names registered on the Internet are basically secondary domain names , such as http://baidu.com、http://taobao.com Wait for the secondary domain name , They are basically managed by enterprises and operation and maintenance personnel . Then there are three or four levels of domain names , I won't go into more details here . Generally speaking, domain name is a mechanism structure from whole to part .

10 Say you are right TIME_WAIT The understanding of the .
Reference answer
appear TIME_WAIT The state and reason of
TIME_WAIT State exists , To ensure the reliability of the network . because TCP The connection is two-way , So when you close the connection , Both directions need to be closed . First hair FIN One side of the package performs active shutdown , Post send FIN One side of the package performs a passive shutdown . The party that voluntarily closes will enter TIME_WAIT state , And stay in this state 2MSL Duration . If Server The end has not been to client End send FIN news ( call close() API), So this CLOSE_WAIT It will always be there .
MSL Concept
It refers to the maximum lifetime of the message segment . If the segment is active in the network MSL Time , Not yet received , Then it will be discarded . About MSL Size ,RFC 793 The advice given in the agreement is 2 minute , however Linux in , Usually half a minute .
TIME_WAIT For two MSL The role of
First , Shut down reliably and safely TCP Connect . For example, network congestion , If the active closing party is the last ACK Not received by the passive shutdown party , At this time, the passive closing party will respond to FIN Perform timeout retransmission , It's not closed yet TIME_WAIT Will get rid of these tail problems , Without affecting new connections and other services . secondly , Prevent... From not continuing TIME_WAIT Time leads to new TCP The connection is established , The delay FIN Retransmission will interfere with new connections .
TIME_WAIT Occupied resources
Small amount of memory ( Probably 4K) And a file descriptor fd.
TIME_WAIT Hazards of shutdown
First , When the network is bad , If the active party does not TIME_WAIT wait for , After closing the previous connection , The active side and the passive side have established a new TCP Connect , At this time, the passive party retransmits or delays FIN The arrival of the package will directly affect the new TCP Connect ; secondly , When the network is bad , At the same time, there is no TIME_WAIT When waiting for , No new connection after closing the connection , Then when the retransmission or delayed message from the passive party is received FIN After package , Will send back one to the passive party RST package , It may affect other service connections of the passive party .
Analysis of the answer
 Insert picture description here
When client The end transmission completes the data , Or when you need to disconnect :
Client Send a FIN Send a message to Server End . Means to terminate Client To Server The connection in this direction . By calling close(socket) API. Express Client No longer send data to Server End .( but Server Can continue to send Client End ).Client The status changes to FIN_WAIT_1.
Server End receipt FIN after , Send a ACK Send a message to Client End ( Serial number is M+1).Server The status changes to CLOSE_WAIT,Client The received serial number is (M+1) Of ACK And then the state becomes FIN_WAIT_2.Server The client also sends a FIN Send a message to Client End .( Serial number is N) Express Server Also end to Client End connection in this direction . By calling close(socket) API.Server The end state changes to LAST_ACK.
Client The terminal receives the message FIN after , Also send a ACK Message to server .( Serial number N+1),Client The status changes to TIME_WAIT.
Server The serial number received by the terminal is (N+1) Of ACK, Server The state of becomes CLOSED.
Such as belt 2MSL after ,Client The state of also changes to CLOSE.
thus , A complete TCP The connection is closed .

11 TIME_WAIT、CLOSE_WAIT At which step does the state occur ?
Reference answer
TIME_WAIT The status occurs when the client actively closes the connection , Send the last ack after ;CLOSE_WAIT The state occurs in Sever End receipt Client Of FIN After message .
appear TIME_WAIT The state and reason of
TIME_WAIT State exists , To ensure the reliability of the network . because TCP The connection is two-way , So when you close the connection , Both directions need to be closed . First hair FIN One side of the package performs active shutdown , Post send FIN One side of the package performs a passive shutdown . The party that voluntarily closes will enter TIME_WAIT state , And stay in this state 2MSL Duration . If Server The end has not been to client End send FIN news ( call close() API), So this CLOSE_WAIT It will always be there .
appear CLOSE_WAIT The state and reason of
Suppose the final ACK The loss of ,server It's going to be retransmitted FIN,client Must maintain TCP Status information so that the final message can be retransmitted ACK, Otherwise... Will be sent RST, result server Think an error has occurred .TCP The implementation must reliably terminate the connection in both directions ( Full duplex off ),client Must enter TIME_WAIT state , because client May face retransmission and eventually ACK The circumstances of .
Why? TIME_WAIT The state needs to be maintained 2MSL For such a long time ?
If TIME_WAIT The state is not maintained long enough ( For example, less than 2MSL), The first connection terminates normally . The second connection with the same related quintuple appears , The duplicate message of the first connection arrives , Interfered with the second connection .TCP The implementation must prevent the repeated message of a connection from appearing after the connection is terminated , So let TIME_WAIT Keep the state long enough (2MSL), Connect... In the corresponding direction TCP The message either responds completely , Or to be discarded . When establishing a second connection , There is no confusion .
Analysis of the answer
 Insert picture description here
When client The end transmission completes the data , Or when you need to disconnect :
Client Send a FIN Send a message to Server End . Means to terminate Client To Server The connection in this direction . By calling close(socket) API. Express Client No longer send data to Server End .( but Server Can continue to send Client End ).Client The status changes to FIN_WAIT_1.
Server End receipt FIN after , Send a ACK Send a message to Client End ( Serial number is M+1).Server The status changes to CLOSE_WAIT,Client The received serial number is (M+1) Of ACK And then the state becomes FIN_WAIT_2.Server The client also sends a FIN Send a message to Client End .( Serial number is N) Express Server Also end to Client End connection in this direction . By calling close(socket) API.Server The end state changes to LAST_ACK.
Client The terminal receives the message FIN after , Also send a ACK Message to server .( Serial number N+1),Client The status changes to TIME_WAIT.
Server The serial number received by the terminal is (N+1) Of ACK, Server The state of becomes CLOSED.
Such as belt 2MSL after ,Client The state of also changes to CLOSE.
thus , A complete TCP The connection is closed .

12 A large number of TIME_WAIT What about the status ?
Reference answer
time_wait The effect of state
TCP Connecting , Actively initiate the closure of one end of the connection , Will enter time_wait state ,time_wait state , The default will last 2 MSL( The maximum lifetime of a message ), It's usually 2x2 mins,time_wait State, ,TCP Connection occupied port , Can't be reused ,TCP Number of ports , The upper limit is 6.5w(65535,16 bit), A lot of time_wait Status exists , It will lead to new construction TCP The connection will go wrong ,address already in use : connect abnormal .
terms of settlement
(1) client :HTTP The head of the request ,connection Set to keep-alive, Stay alive for a while : Today's browsers , It's usually done like this .
(2) Server side
a. allow time_wait State of socket Be reused
b. cut time_wait Time , Set to 1 MSL( namely ,2 mins)

13 Please introduce socket Specific steps of communication .
Reference answer
sockets( Socket ) There are three kinds of programming : Streaming socket (SOCK_STREAM), Socket datagram (SOCK_DGRAM), Raw socket (SOCK_RAW); be based on TCP Of socket Programming is using streaming socket .
Server side programming steps
(1) Load socket library , Create socket (WSAStartup()/socket());
(2) Bind socket to a IP Address and a port (bind());
(3) Set the socket to listening mode to wait for a connection request (listen());
(4) When the request comes , Accept connection request , Returns a new socket corresponding to this connection (accept());
(5) Use the socket returned to communicate with the client (send()/recv());
(6) return , Waiting for another connection request ;
(7) Close socket , Close the loaded socket library (closesocket()/WSACleanup()).
Client programming steps :
(1) Load socket library , Create socket (WSAStartup()/socket());
(2) Send a connection request to the server (connect());
(3) Communicate with the server (send()/recv());
(4) Close socket , Close the loaded socket library (closesocket()/WSACleanup()).
Analysis of the answer

// Code instance ( The server ) 
#include <stdio.h> 
#include <Winsock2.h> 
void main() {

WORD wVersionRequested;
WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
 return; }
if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) {
 WSACleanup( );
return;
} SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); listen(sockSrv,5);
SOCKADDR_IN addrClient; int len=sizeof(SOCKADDR); while(1) {
 SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[50]; sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr)); send(sockConn,sendBuf,strlen(sendBuf)+1,0); char recvBuf[50]; recv(sockConn,recvBuf,50,0); printf("%s\n",recvBuf); closesocket(sockConn); } }
// Code instance ( client ) 
#include <stdio.h> 
#include <Winsock2.h> void main() {
 
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
 return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) {
 WSACleanup( );
return; }
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000); connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
send(sockClient,"hello",strlen("hello")+1,0); char recvBuf[50];
recv(sockClient,recvBuf,50,0); printf("%s\n",recvBuf);
closesocket(sockClient); WSACleanup(); }

14 How can the server improve the processing socket Performance of connections ?
Reference answer
Improve processing socket Performance of connections , Follow these tips :
Minimize the delay of message transmission .
Minimize the load of system calls .
by Bandwidth Delay Product Adjust the TCP window .
Dynamic optimization GNU/Linux TCP/IP Stack .
Analysis of the answer
Minimize the delay of message transmission .
Through TCP socket When communicating , The data is split into data blocks , In this way, they can be encapsulated into a given connection TCP payload( finger TCP Payload in the packet ) It's in .TCP payload The size of depends on several factors ( For example, maximum message length and path ), But these factors are known at the time of connection initiation . For best performance , Our goal is to populate each message with as much available data as possible . When there is not enough data to fill payload when ( Also known as maximum segment length (maximum segment size) or MSS),TCP Will adopt Nagle The algorithm automatically connects some small buffers to a message segment . This can improve the efficiency of the application by minimizing the number of messages sent , And reduce the overall network congestion problem .
Minimize the load of system calls .
Any time through a socket When reading and writing data , Are using a system call (system call). This call ( for example read or write) Across the boundaries of user space applications and the kernel . in addition , Before entering the kernel , The call will pass C Library to enter a general function in the kernel (system_call()). from system_call() in , This call goes into the file system layer , Here the kernel determines which type of device is being processed . Last , The call will enter socket layer , Data is read or queued here to pass socket Transmitting ( This involves a copy of the data ).
This process shows that system calls do not just operate in the application and kernel , And it has to go through many levels in the application and kernel . This process costs a lot of resources , So the more calls , The longer it takes to work through this call chain , The lower the performance of the application . Because we can't avoid these system calls , Therefore, the only option is to minimize the number of times these calls are used .
by Bandwidth Delay Product Adjust the TCP window .
TCP The performance of depends on several factors . The two most important factors are link bandwidth (link bandwidth)( The rate at which messages are transmitted over the network ) and Round trip time (round-trip time) or RTT( The delay between sending a message and receiving a response from the other end ). These two values determine what is called Bandwidth Delay Product(BDP) The content of .
Given link bandwidth and RTT after , You can figure it out BDP The value of the , But what does that mean ?BDP A simple method is given to calculate the theoretically optimal TCP socket Buffer size ( It holds the data queued for transmission and waiting for the application to receive ). If the buffer is too small , that TCP The window cannot be fully opened , This limits performance . If the buffer is too large , Then valuable memory resources will be wasted . If the set buffer size is just right , Then you can make full use of the available bandwidth .
Dynamic optimization GNU/Linux TCP/IP Stack .
The standard GNU/Linux The distribution tries to optimize all deployment scenarios . This means that the standard release may not specifically optimize the existing environment .GNU/Linux Provides many adjustable kernel parameters , These parameters can be dynamically configured for your own operating system .

15 Introduce flow control and congestion control .
Reference answer
Definition of flow control and congestion control
flow control
If the sender sends the data too fast , The receiver may not have time to receive , This will result in the loss of data . Flow control is to make the sender slow down , To give the receiver time to receive .
Congestion control
Congestion control is to prevent too much data from being injected into the network , In this way, the router or link in the network will not be overloaded .
The difference between flow control and congestion control
Flow control is end-to-end control , for example A Through the Internet B Sending data ,A Sending too fast leads to B Can't accept (B Buffer window is too small or processing is too slow ), The control at this time is flow control , The principle is to change the size of the sliding window .
Congestion control is A And B The network between them is blocked, resulting in slow transmission or packet loss , No time to transmit . Prevent too much data from being injected into the network , In this way, the router or link in the network will not be overloaded . Congestion control is a global process , All the hosts involved 、 Router , And all the factors related to the degradation of network performance .
TCP Flow control solution
TCP The flow control of is realized by sliding window mechanism , The receiver will include the size of its own receiving window in the returned data , To control the data transmission of the sender .
TCP Congestion control solutions
TCP Four algorithms of congestion control : Slow start 、 Congestion avoidance 、 Fast retransmission 、 Fast recovery .
(1) Slow start algorithm : When the host starts sending data , I don't know the load of the network , Therefore, the congestion window gradually increases from small to large , The congestion window is doubled every time there is no timeout after a transmission round . At the same time, a slow start threshold needs to be set , The slow start algorithm is used when the congestion window is less than the slow start threshold , Greater than the slow start threshold , Use congestion avoidance algorithms ;
(2) Congestion avoidance algorithm : When the congestion window is greater than the slow start threshold , Let the congestion window grow slowly according to the linear law . That is, every transmission round , The congestion window increases by one MSS Maximum segment size .( Congestion avoidance is not entirely able to avoid congestion , It just makes the network less prone to congestion )
(3) Fast retransmission algorithm : Let the sender know that individual message segments have been lost this morning , There is no network congestion .
It is required to accept the request. Do not confirm the piggyback when you send data by yourself , It's sending an immediate confirmation , Even if the out of order message segment is received, the repeated confirmation of the received message segment shall be sent immediately . Once the sender receives three consecutive reread confirmations , Immediately retransmit the corresponding message segment .
(4) Fast recovery algorithm : When the sender knows that only individual segments are lost rather than network congestion , Do not start slow start algorithm , It's a fast recovery algorithm , Adjust the slow start threshold and congestion window value to half of the current window , Start congestion avoidance algorithm

16 Know something about routing protocols ?
Reference answer
I understand .
Routing protocol definition
Routing protocol ( English :Routing protocol) It is a kind of internet protocol that specifies the way of data packet transfer .Internet The main node equipment of the network is the router , The router forwards the received data through the routing table . The forwarding policy can be manually specified ( Through static routing 、 Policy routing and other methods ). In a network with a smaller scale , There is no problem with manually specifying the Forwarding Policy . But in a larger network ( Such as multinational enterprise network 、ISP The Internet ), If the forwarding policy is manually specified , It will bring huge workload to network administrators , And managing 、 It also becomes very difficult to maintain the routing table . To solve this problem , Dynamic routing protocol came into being . Dynamic routing protocol allows routers to automatically learn from the networks of other routers , And automatically update the routing table after the network topology changes . The network administrator only needs to configure the dynamic routing protocol , Compared with manually specifying the Forwarding Policy , The workload is greatly reduced .
principle
Routing protocols support routable protocols by sharing routing information between routers . Routing information is passed between adjacent routers , Make sure all routers know the path to other routers . All in all , The routing protocol creates a routing table , Describe the network topology ; Routing protocol and router , Perform routing and packet forwarding functions .
The role of routers and common routing protocols
The routing protocol mainly runs on the router , Routing protocol is used to determine the arrival path , Play a map navigation , Responsible for finding the way . It works on the network layer . It includes RIP,IGRP(Cisco Private agreements ),EIGRP(Cisco Private agreements ),OSPF,IS-IS,BGP. The following is a detailed description of these six agreements :
(1)RIP( Routing information protocol )
RIP It has long been used in Internet On , Is the simplest routing protocol . It is “ Routing information protocol (Route Information Protocol)” Abbreviation , It mainly transmits routing information , By every 30 Broadcast the routing table once a second , Maintain the location relationship of adjacent routers , At the same time, calculate your own routing table information according to the received routing table information .RIP It's a distance vector routing protocol , The maximum number of hops is 15 jump , exceed 15 The hopping network considers the target network unreachable . This protocol is usually used in small network environment with simple network architecture . It is divided into RIPv1 and RIPv2 Two versions , The latter supports VLSM Technology and a series of technical improvements .RIP The convergence speed is slow .
(2)IGRP( Internal Gateway Routing Protocol )
IGRP Agreement is “ Internal Gateway Routing Protocol (Interior Gateway Routing Protocol)” Abbreviation , from Cisco Developed independently in the 1980s , Belong to Cisco Private agreements .IGRP and RIP equally , Same distance vector routing protocol , Therefore, there are similarities in many aspects , Such as IGRP It is also a periodic broadcast routing table , There is also a maximum number of hops ( The default is 100 jump , To achieve or surpass 100 Jump means that the target network is unreachable ).IGRP The biggest feature is the use of mixed measures , At the same time, the bandwidth of the link is considered 、 Delay 、 load 、MTU、 reliability 5 Two aspects to calculate the measurement of routing , Not like others IGP The protocol simply considers one aspect to calculate the measurement value .IGRP Has been Cisco Independently developed EIGRP The agreement replaces , The version number is 12.3 And above Cisco IOS(Internetwork Operating System) The protocol is no longer supported , It has been rare to run IGRP The network of protocols .
(3)EIGRP( Enhanced internal gateway routing protocol )
because IGRP Various defects and deficiencies of the agreement ,Cisco Developed EIGRP agreement ( Enhanced internal gateway routing protocol ) To replace IGRP agreement .EIGRP It belongs to advanced distance vector routing protocol ( Also known as hybrid routing protocol ), Inherited IGRP Mixed measure of , The biggest feature is the introduction of non equivalent load balancing technology , And has a very fast convergence speed .EIGRP The agreement Cisco Devices are widely deployed in the network environment .
(4)OSPF( Open shortest paths take precedence )
OSPF Agreement is “ Open shortest paths take precedence (Open Shortest Path First)” Abbreviation , It belongs to link state routing protocol .OSPF Put forward “ Area (area)” The concept of , All routers in each area maintain the same link state database (LSDB). The region is divided into backbone regions ( The number of backbone area must be 0) And non backbone areas ( Not 0 Number area ), If one runs OSPF There is only a single area in the network , Then the area can be a backbone area or a non backbone area . If there are multiple areas in the network , Then there must be a backbone area , And all non backbone areas must be directly connected to the backbone area .OSPF Use the maintained link state database , Through the shortest path first algorithm (SPF Algorithm ) Calculate the routing table .OSPF The convergence speed of . Because of its unique openness and good expansibility ,OSPF Protocols are widely deployed in various networks .
(5)IS-IS( Intermediate system to intermediate system )
IS-IS Agreement is Intermediate system to intermediate system( Intermediate system to intermediate system ) Abbreviation , It belongs to link state routing protocol . standard IS-IS The agreement was formulated by the international organization for standardization ISO/IEC 10589:2002 Defined by the , standard IS-IS Not suitable for IP The Internet , therefore IETF Formulated for IP Network integration IS-IS agreement (Integrated IS-IS). and OSPF identical ,IS-IS Also used. “ Area ” The concept of , It also maintains a link state database , Through the shortest spanning tree algorithm (SPF) Calculate the best path .IS-IS The convergence speed of . Integration IS-IS Agreement is ISP The most commonly used on the backbone network IGP agreement .
(6)BGP( Border gateway protocol )
In order to maintain each ISP The independent interests of , The standardization organization has formulated ISP Routing protocol between BGP.BGP yes “ Border gateway protocol (Border Gateway Protocol)” Abbreviation , Processing each ISP Routing between . however BGP Operate in a relatively core position , Users need to have a good understanding of the structure of the network , Otherwise, it may cause great losses .

17 What kind of protocol may be used for live broadcasting ?
Reference answer
There are many protocols for live video , Use rtmp The point of the agreement is rtmp live broadcast . A live stream is a video stream , That is, the transmitted video data . Common protocols are RTMP、RTSP、HTTP agreement , These three protocols belong to the Internet TCP/IP Protocol of application layer in five layer architecture . Theoretically, all three can be used for live video or on-demand . But generally speaking , Live broadcast usually uses RTMP、RTSP, And on demand HTTP. Here are the characteristics of the three .
RTMP agreement
(1) It's the streaming protocol ;
(2)RTMP Agreement is Adobe Private agreement of , Not fully public ;
(3)RTMP Protocols generally transmit flv,f4v Format stream ;
(4)RTMP Generally in TCP1 Transfer commands and data on multiple channels .
RTSP agreement
(1) It's the streaming protocol ;
(2)RTSP An agreement is a common agreement , And there are special agencies to do maintenance ;
(3)RTSP Protocols generally transmit ts、mp4 The stream of formats ;
(4)RTSP Transmission generally requires 2-3 Channels , Command and data channels are separated .
HTTP agreement
(1) No, it's a streaming protocol ;
(2)HTTP An agreement is a common agreement , And there are special agencies to do maintenance ;
(3)HTTP The protocol has no specific transport stream ;
(4)HTTP Transmission generally requires 2-3 Channels , Command and data channels are separated .
Analysis of the answer
Extended data
A complete live video process , Including acquisition 、 Handle 、 code 、 encapsulation 、 Push flow 、 transmission 、 transcoding 、 distribution 、 decode 、 Play, etc .
collection
Audio acquisition the audio acquisition process mainly collects the analog signals in the environment into PCM Encoded raw data , Then the code is compressed into MP3 And so on . Common audio compression formats are :MP3,AAC,HE-AAC,Opus,FLAC,Vorbis (Ogg),Speex and AMR etc. .
Image acquisition The image acquisition process is mainly captured by cameras and other equipment YUV Encoded raw data , Then it is encoded and compressed into H.264 And so on . Common video packaging formats are :MP4、3GP、AVI、MKV、WMV、MPG、VOB、FLV、SWF、MOV、RMVB and WebM etc. .
Handle
Get the original data after video or audio collection , In order to enhance some live effects or add some additional effects , We usually deal with it before we compress it .
video : Skin care 、 watermark 、 route 、 Customize .
Audio : Mixing 、 Noise reduction 、 Special effects 、 Customize .
code
For streaming media transmission , Coding is very important , Its coding performance 、 Coding speed and coding compression ratio will directly affect the user experience and transmission cost of the whole streaming media transmission .
Common video coders :
(1)H.264/AVC
(2)HEVC/H.265
(3)VP8
(4)VP9
(5)FFmpeg
Audio coder :Mp3, AAC etc. .
encapsulation
Put the multimedia content generated by the encoder ( video , Audio , subtitle , Chapter information, etc ) Mixed packaging together several common packaging formats :
(1)AVI Format ( The suffix is .avi)
(2)DV-AVI Format ( The suffix is .avi)
(3)QuickTime File Format Format ( The suffix is .mov)
(4)MPEG Format ( The file suffix can be .mpg .mpeg .mpe .dat .vob .asf .3gp .mp4 etc. )
(5)WMV Format ( The suffix is .wmv .asf)
(6)Real Video Format ( The suffix is .rm .rmvb)
(7)Flash Video Format ( The suffix is .flv)
(8)Matroska Format ( The suffix is .mkv)
(9)MPEG2-TS Format ( The suffix is .ts)
at present , We're streaming , Especially in the live broadcast FLV and MPEG2-TS Format , for RTMP/HTTP-FLV and HLS agreement .
Push flow
Streaming refers to the process of transmitting live content to the server using content capture software such as streaming tools . There are three main push protocols :
(1)RTSP(Real Time Streaming Protocol): Real time streaming protocol , It's a multimedia streaming protocol used to control sound or video , from Real Networks and Netscape Jointly proposed ;
(2)RTMP(Real Time Messaging Protocol): Real time messaging protocol , yes Adobe Company as Flash Audio between player and server 、 Video and data transmission Open protocols developed ;
(3)HLS(HTTP Live Streaming): It's apple (Apple Inc.) Based on HTTP Streaming media transport protocol of ;RTMP Is the current mainstream streaming media transmission protocol , Widely used in live broadcasting , Most of the products on the market have adopted this protocol .
RTMP Based on the agreement TCP, It is a network protocol designed for real-time data communication , Mainly used in flash/AIR Platform and support RTMP Protocol streaming / Audio video and data communication between interactive servers . The software supporting the protocol includes Adobe Media Server/Ultrant Media Server/red5 etc. . It has three varieties :
(1)RTMP Working in TCP Clear text protocol above , The use of port 1935;
(2)RTMPT Packaged in HTTP In request , Traversable firewall ;
(3)RTMPS similar RTMPT, But it uses HTTPS Connect ;
RTMP Protocol is like a container for packets , The data can be AMF Formatted data , It can also be FLV Medium vision / Audio data . A single connection can transmit multiple network streams over different channels . The packets in these channels are transmitted in fixed size packets .
transmission
The streaming media pushed out needs to be transmitted to the audience , The whole link is the transmission network .
transcoding
The bit rate of the live video streaming end is determined according to the streaming end , That is, the bit rate at the streaming end is consistent with that at the streaming end . However, the following scenarios will cause poor live broadcast effect : The bit rate at the streaming end does not match the bandwidth at the streaming end . When the bit rate of the streaming end is high and the bandwidth resources of the client are limited, the playback will get stuck , When the bit rate of the streaming end is low, but the client has high requirements for live broadcasting efficiency, the playback effect will be poor . The player plug-in needs to realize multi bit rate switching . The front-end player plug-in can often set bit rate switching , This requires that the same push stream can provide multicast addresses with multiple bit rates at the same time . therefore , Live video broadcasting provides real-time transcoding function and provides services for multiple streaming addresses with different code rates at the same time .
distribution
The function of streaming media server is to be responsible for the publishing, rebroadcasting and distribution of live streaming .
decode
Encoder (Encoder): A device or program that compresses signals ;
decoder (Decoder): A device or program that decompresses signals ;
codecs (Codec): The codec is right .
Player streaming mainly realizes the display of live programs on the terminal . Because the transmission protocol used here is RTMP, So just support RTMP All players of streaming protocol can use .

18 Talk about single work 、 Duplex 、 Half duplex communication .
Reference answer
Simplex : Data transmission only supports data transmission in one direction ; Only one party can receive or send messages at the same time , Two way communication is not possible . give an example : TV , radio broadcast .
Half duplex : Half duplex data transmission allows data to be transmitted in two directions , however , At some point , Only data can be transmitted in one direction , It's actually a kind of direction switching simplex communication ; Only one party can receive or send information at the same time , Can achieve two-way communication . give an example : Walkie-talkie .
Duplex : Full duplex data communication allows data to be transmitted in both directions at the same time , therefore , Full duplex communication is a combination of two simplex communication modes , It requires that both the transmitting device and the receiving device have independent receiving and transmitting capabilities ; Information can be received and sent at the same time , Two way communication . give an example : Telephone communication .
Analysis of the answer
Extended data :
Simplex 、 Half duplex and full duplex are three communication channels in telecommunication computer network . These communication channels can provide a way to convey information . The communication channel can be a physical transmission medium or a logical connection through a multiplexing medium . Physical transmission medium refers to the material that can transmit energy waves , For example, wires in data communication . And logical connection usually refers to circuit switching connection or packet mode virtual circuit connection , For example, wireless telecommunication channel . With the help of communication channel , Information can be transmitted without obstacles .
Simplex mode is generally used to transmit data in only one direction . For example, the communication between computer and printer is simplex mode , Because only computers transmit data to printers , Without data transmission in the opposite direction . And in some communication channels , Such as simplex wireless transmission, etc .
Welcome to join me for wechat exchange and discussion ( Please note csdn Add )

 Insert picture description here

Copyright statement
In this paper,the author:[Duan Xuechang],Reprint please bring the original link, thank you