The content of the invention
To solve above-mentioned technical problem, the present invention provides a kind of connection establishment method and server, can be by controlThe time that socket sets up slows down the initial time in keep Alive Packet cycle with this, so that server issues the time of keep Alive PacketAveragely, network burden is mitigated.
The technical scheme that the present invention is provided is as follows:
The invention discloses a kind of connection establishment method, including step:S100, determine whether client initiate connection pleaseAsk;S200, when have client initiate connection request when, judge whether current client access amount reaches predetermined threshold value;It is describedPredetermined threshold value is less than client total quantity;If so, execution step S300;Otherwise, execution step S400;When S300, wait are defaultThe connection request of client is received after length, the connection with client is set up;S400, the connection request for receiving client, set upWith the connection of client.
The present invention first carries out judging whether active client access amount reaches default threshold when client initiates connection requestValue, only can just receive the connection request of client when client access amount is not up to predetermined threshold value, set up and clientConnection, so as to control the setup time of socket, with this initial time in keep Alive Packet cycle can be slowed down, it is to avoid the same timeDuring a large amount of client connection servers, the server same time sends keep Alive Packet, causes the downlink bandwidth utilization rate of serverInequality, therefore the present invention can effectively mitigate network burden.
It is further preferred that also including step before step S100:S010, with the preset duration as the cycle updateClient access amount in each described preset duration.
Timing of the invention updates the client access amount in each preset duration, for avoiding in each preset duration simultaneouslyExcessive client is accessed, causes the initial time in the cycle of keep Alive Packet transmission identical.
It is further preferred that also including step before step S100:S020, execution initialization program, obtain describedPredetermined threshold value.
It is further preferred that step S020 further includes step:S021, original Accept functions are changedWrite, default access rate parameter is increased in the original Accept functions, and the default access rate parameter is set;S022, the predetermined threshold value is obtained according to the default access rate parameter.
It is further preferred that the default access rate parameter and the predetermined threshold value are calculated as follows;R=N/T;Y=R*S;Wherein, R represents default access rate parameter, and N represents client total quantity, and T represents the keep Alive Packet cycle, and Y represents pre-If threshold value, S represents preset duration.
The invention also discloses a kind of server, including:Request judge module, for determining whether that client is initiated to connectConnect request;Access amount judge module, for when there is client to initiate connection request, whether judging current client access amountReach predetermined threshold value;The predetermined threshold value is less than client total quantity;Connection establishment module, for waiting being followed by for preset durationThe connection request of client is received, the connection with client is set up;And the connection request of reception client, set up and clientConnection.
It is further preferred that also including:Counting module is updated, for updating described in each as the cycle with the preset durationClient access amount in preset duration.
It is further preferred that also including:Acquisition module, for performing initialization program, obtains the predetermined threshold value.
It is further preferred that the acquisition module is further included:Function setup submodule, for original Accept lettersNumber is rewritten, and default access rate parameter is increased in the original Accept functions, and arranges the default access rateParameter;Calculating sub module, for obtaining the predetermined threshold value according to the default access rate parameter.
It is further preferred that the default access rate parameter and the predetermined threshold value are calculated as follows;R=N/T;Y=R*S;Wherein, R represents default access rate parameter, and N represents client total quantity, and T represents the keep Alive Packet cycle, and Y represents pre-If threshold value, S represents preset duration.
Compared with prior art, the present invention first carries out judging active client access amount when client initiates connection requestPredetermined threshold value whether is reached, the connection request of client only can be just received when client access amount is not up to predetermined threshold value,Set up and the connection of client, so as to control the setup time of socket, with this can slow down the keep Alive Packet cycle it is initial whenBetween, it is to avoid during same time a large amount of client connection servers, the server same time sends keep Alive Packet, causes serverDownlink bandwidth utilization rate is unequal, therefore the present invention can effectively mitigate network burden.
Specific embodiment
In order to be illustrated more clearly that the embodiment of the present invention or technical scheme of the prior art, below by control description of the drawingsThe specific embodiment of the present invention.It should be evident that drawings in the following description are only some embodiments of the present invention, forFor those of ordinary skill in the art, on the premise of not paying creative work, can be obtaining other according to these accompanying drawingsAccompanying drawing, and obtain other embodiments.
To make simplified form, part related to the present invention is only schematically show in each figure, they are not representedIts practical structures as product.In addition, so that simplified form is readily appreciated, with identical structure or function in some figuresPart, only symbolically depicts one of those, or has only marked one of those.Herein, " one " is not only represented" only this ", it is also possible to represent the situation of " more than one ".
Fig. 1 is a kind of key step schematic diagram of connection establishment method of the invention, as shown in figure 1, a kind of connection establishment sideMethod, including step:S100, determine whether client initiate connection request;S200, when have client initiate connection request when,Judge whether current client access amount reaches predetermined threshold value;The predetermined threshold value is less than client total quantity;If so, performStep S300;Otherwise, execution step S400;S300, the connection request for waiting reception client after preset duration, set up and visitorThe connection at family end;S400, the connection request for receiving client, set up the connection with client.
Specifically, in linux system, a Socket only one of which KeepAlive cycle time parameter, i.e. serverThe period distances for being sent to the KeepAlive messages of all of client are identicals, and initial time is when being exactly the access of equipmentBetween.It is of the invention first to carry out judging whether active client access amount reaches predetermined threshold value when client initiates connection request, onlyHaving can just receive the connection request of client when client access amount is not up to predetermined threshold value, set up the connection with client,So as to control the setup time of socket, the initial time in keep Alive Packet cycle can be slowed down with this, it is to avoid the same time is a large amount ofDuring client connection server, the server same time sends keep Alive Packet, causes the downlink bandwidth utilization rate of server uneven, therefore the present invention can effectively mitigate network burden.
Preferably, step is also included before step S100:S010, update each institute by the cycle of the preset durationState the client access amount in preset duration.
Specifically, present invention timing updates the client access amount in each preset duration, during for avoiding each defaultAccess excessive client in length simultaneously, cause the initial time in the cycle of keep Alive Packet transmission identical.
Preferably, step is also included before step S100:S020, execution initialization program, obtain the default thresholdValue.
Predetermined threshold value is user according to real network situation self-defining in the present invention, for receiving client transmission companyAs the standard for judging when connecing request, as long as predetermined threshold value is less than client total quantity, you can reduce part client socketSetup time, so as to slow down the initial time in keep Alive Packet cycle.
Preferably, step S020 further includes step:S021, original Accept functions are rewritten, in instituteState and increase in original Accept functions default access rate parameter, and the default access rate parameter is set;S022, according to instituteState default access rate parameter and obtain the predetermined threshold value.
Specifically, the process that socket three-way handshakes are set up is as follows:
A. user end to server sends a SYN J
B. server is to one SYN K of client end response, and SYN J are carried out to confirm ACK J+1
C. user end to server sends a confirmation ACK K+1
Client call Connect functions initiate three-way handshake, and server is then blocked in Accept functions here, from stepAccept functions are constantly in blocked state between B to C, until receiving step C in ACK be just that Socket completes to buildIt is vertical.The present invention is improved to Accept functions so that Accept functions can be supported to arrange default access rate parameter, i.e., oftenSecond, maximum client accessed the parameter of quantity.In Linux, original Accept function prototypes are as follows:
int accept(int sockfd,struct sockaddr*addr,socklen_t*addrlen);
Wherein sockfd is the socket of socket, and addr is the address data structure of client.The present invention is by this letterNumber is improved to int accept (int sockfd, struct sockaddr*addr, int accept_rate, socklen_t*addrlen);Default access rate parameter int accept_rate is increased, the effect of the default access rate parameter is controlThe speed that client is accessed, i.e. how many clients of access per second.
The present invention presets access rate parameter by increasing, when can know default according to the default access rate parameterThe predetermined threshold value for accessing client is allowed in long.
Preferably, the default access rate parameter and the predetermined threshold value are calculated as follows;R=N/T;Y=R*S;Wherein, R represents default access rate parameter, and N represents client total quantity, and T represents the keep Alive Packet cycle, and Y represents default thresholdValue, S represents preset duration.
The keep Alive Packet cycle for assuming server is T, has N platform client devices to be connected to server, then preset and access speedRate parameter is then N/T, and according to N/T speed client is accessed, then the predetermined threshold value that access client is allowed in preset duration is thenS*N/T, and it is preferred, and ideally, the time point of this N platform equipment access server is generally evenly distributed in the keep Alive Packet cycleIn T, i.e., preset duration is 1 second, then the quantity of access server per second is N/T platforms.
Fig. 2 is a kind of flow chart of connection establishment method of the invention.As shown in Fig. 2 the workflow of the present invention is as follows:
20th, start.
21st, determine whether that client initiates connection request;If so, next step is performed;Otherwise execution step 21;
22nd, judge whether current client access amount reaches predetermined threshold value;If so, execution step 23;Otherwise, step is performedRapid 24.
23rd, the connection request that client is received after preset duration is waited, the connection with client is set up.
24th, the connection request of client is received, the connection with client is set up.
Fig. 3 is a kind of main composition schematic diagram of server of the invention.As shown in figure 3, a kind of server, including:RequestJudge module 100, for determining whether that client initiates connection request;Access amount judge module 200, has client for working asWhen initiating connection request, judge whether current client access amount reaches predetermined threshold value;The predetermined threshold value is less than clientTotal quantity;Connection establishment module 300, for waiting preset duration after receive client connection request, set up and clientConnection;And the connection request of reception client, set up the connection with client.
The present invention first carries out judging whether active client access amount reaches default threshold when client initiates connection requestValue, only can just receive the connection request of client when client access amount is not up to predetermined threshold value, set up and clientConnection, so as to control the setup time of socket, with this initial time in keep Alive Packet cycle can be slowed down, it is to avoid the same timeDuring a large amount of client connection servers, the server same time sends keep Alive Packet, causes the downlink bandwidth utilization rate of serverInequality, therefore the present invention can effectively mitigate network burden.
Fig. 4 is that a kind of server of the invention is fully composed schematic diagram.As shown in Figure 4, it is preferred that also include:Update meterDigital-to-analogue block 400, for updating the client access amount in each described preset duration as the cycle with the preset duration.
Counting module 400 is updated in the present invention first to reset in initialization, then records the client's termination in preset durationEnter amount, while when next preset duration arrives, removing the client access amount of previous preset duration.
Preferably, as shown in figure 4, also including:Acquisition module 500, for performing initialization program, obtains the default thresholdValue.
Predetermined threshold value is user according to real network situation self-defining in the present invention, for receiving client transmission companyAs the standard for judging when connecing request, as long as predetermined threshold value is less than client total quantity, you can reduce part client socketSetup time, so as to slow down the initial time in keep Alive Packet cycle.
Preferably, the acquisition module 500 is further included:Function setup submodule 510, for original Accept lettersNumber is rewritten, and default access rate parameter is increased in the original Accept functions, and arranges the default access rateParameter;Calculating sub module 520, for obtaining the predetermined threshold value according to the default access rate parameter.
Preferably, the default access rate parameter and the predetermined threshold value are calculated as follows;R=N/T;Y=R*S;Wherein, R represents default access rate parameter, and N represents client total quantity, and T represents the keep Alive Packet cycle, and Y represents default thresholdValue, S represents preset duration.
The keep Alive Packet cycle for assuming server is T, has N platform client devices to be connected to server, then preset and access speedRate parameter is then N/T, and according to N/T speed client is accessed, then the predetermined threshold value that access client is allowed in preset duration is thenS*N/T, and it is preferred, and ideally, the time point of this N platform equipment access server is generally evenly distributed in the keep Alive Packet cycleIn T, i.e., preset duration is 1 second, then the quantity of access server per second is N/T platforms.
It should be noted that content and the said method such as information exchange, implementation procedure in book server between each moduleEmbodiment is based on same design, and particular content can be found in the narration in the inventive method embodiment, and here is omitted.
It should be noted that above-described embodiment can independent assortment as needed.The above is only the preferred of the present inventionEmbodiment, it is noted that for those skilled in the art, in the premise without departing from the principle of the inventionUnder, some improvements and modifications can also be made, these improvements and modifications also should be regarded as protection scope of the present invention.