Summary of the invention
Technical problem to be solved by this invention provides a kind of method for reliable transmission of data, can guarantee on the basis of efficient transfer data, solves the problem that existing udp protocol can not reliable transmitting data.
The present invention also provides a kind of reliable transmission device of data, to guarantee said method application in practice.
In order to address the above problem, the invention discloses a kind of method for reliable transmission of data, comprising: the file that data sending terminal will send splits into packet, and for each packet is provided with sequence number, organizes the foundation of bag again as data receiver; By default buffered data packet format the above-mentioned data envelope that is provided with sequence number is dressed up packet to be sent, and be added into the transmission buffering area; Send the packet to be sent in the described transmission buffering area in order; Delete the packet that data receiver has been confirmed in the described transmission buffering area; Whether the time of staying of judging packet in the described transmission buffering area surpasses default time threshold, if, then this packet is reset to packet to be sent, repeat above-mentioned process of transmitting.
Preferably, before the packet to be sent in sending described transmission buffering area, also comprise: data envelope to be sent is dressed up the transmission packet by default protocol header form.
Preferably, also comprise: when the sending order of described transmission buffering area when increasing progressively from small to large by sequence number, if the packet that described data receiver receives is the expectation packet, then the packet in the described reception buffering area is pressed the sequence number ordering, and the maximum sequence number that the expected data packet number changes the continuous packet of sequence number into added 1, and, by default protocol header form this maximum sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into the reception buffering area; Wherein, all packets that are less than or equal to sequence number in the described response data packet are the packet that described data receiver acknowledges receipt of;
When the sending order of described transmission buffering area when successively decreasing from big to small by sequence number, if the packet that described data receiver receives is the expectation packet, then the packet in the described reception buffering area is pressed the sequence number ordering, and the minmal sequence that the expected data packet number changes the continuous packet of sequence number into number subtracted 1, and, by default protocol header form this minmal sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into the reception buffering area; Wherein, all packets more than or equal to sequence number in the described response data packet are the packet that described data receiver acknowledges receipt of.
Preferably, also comprise: when the packet that will need to retransmit resets to packet to be sent, write down its number of retransmissions; If the number of retransmissions of retransmission data packet may is all greater than default number of retransmissions higher limit in the described transmission buffering area, then be that described default time threshold increases a fixed value; If the number of retransmissions of retransmission data packet may is all less than default number of retransmissions lower limit in the described transmission buffering area, then be that described default time threshold reduces by a fixed value.
Preferably, the data item of described default buffered data packet comprises: sequence number, timestamp and the number of retransmissions of the data that transmit, data length, transmission packet.
Preferably, the data item of described default protocol header comprises: the command word of bag sign, upper-layer protocol, send sequence number, sequence number, the data length of reply data bag and the data that will transmit of packet.
Preferably, before being added into described transmission buffering area, described packet to be sent also comprises: judge whether described transmission buffering area fills up, if then abandon this packet to be sent.
According to another preferred embodiment of the present invention, a kind of reliable transmission device of data is also disclosed, comprise data sending terminal and data receiver, described data sending terminal is connected by network with data receiver, described data sending terminal comprises data split cells, buffered data packet encapsulation unit, data transmission unit, response confirmation unit, packet reset cell and sends buffering area, wherein: the file that described data split cells is used for sending splits into packet, and, organize the foundation of bag again as described data receiver for each packet is provided with sequence number; Described buffered data encapsulation unit is used for by default buffered data packet format the data envelope that is provided with sequence number that described data split cells generates being dressed up packet to be sent, and is added into described transmission buffering area; Described data transmission unit is used in order the packet to be sent of described transmission buffering area is sent to described data receiver by described network; The packet that described response confirmation unit is used for described data receiver has been confirmed is deleted from described transmission buffering area; Described packet reset cell is used for judging that whether the time of staying of described transmission buffering area packet is above default time threshold, if then this packet is reset to packet to be sent; Described transmission buffering area is used to preserve the packet to be sent that described buffered data packet encapsulation unit generates, and the packet that described data transmission unit sends, described response confirmation unit does not also have deletion.
Preferably, described data sending terminal also comprises the protocol package encapsulation unit, is used to described data transmission unit by default protocol header form the data envelope to be sent in the described transmission buffering area to be dressed up the transmission packet.
Preferably, described data receiver is provided with and receives buffering area and Data Receiving processing unit, and wherein: described reception buffering area is used to preserve the packet that described Data Receiving processing unit receives;
Described Data Receiving processing unit is used for, when the sending order of described data sending terminal when increasing progressively from small to large by sequence number, judge whether the packet that described data receiver receives is the expectation packet, if, then the packet in the described reception buffering area is pressed the sequence number ordering, and the maximum sequence number that the expected data packet number changes the continuous packet of sequence number into added 1, and, by default protocol header form this maximum sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into described reception buffering area; Wherein, all packets that are less than or equal to sequence number in the described response data packet are the packet that described data receiver acknowledges receipt of;
When the sending order of described data sending terminal when successively decreasing from big to small by sequence number, judge whether the packet that described data receiver receives is the expectation packet, if, then the packet in the described reception buffering area is pressed the sequence number ordering, and the minmal sequence that the expected data packet number changes the continuous packet of sequence number into number subtracted 1, and, by default protocol header form this minmal sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into the reception buffering area; Wherein, all packets more than or equal to sequence number in the described response data packet are the packet that described data receiver acknowledges receipt of.
Preferably, described data sending terminal also comprises the parameter adjustment unit, is used for writing down its number of retransmissions when the packet that described packet reset cell will need to retransmit resets to packet to be sent; And whether the number of retransmissions of judging retransmission data packet may in the described transmission buffering area is all greater than default number of retransmissions higher limit, if then be that described default time threshold increases a fixed value; And whether the number of retransmissions of judging retransmission data packet may in the described transmission buffering area is all less than default number of retransmissions lower limit, if then be that described default time threshold reduces by a fixed value.
Preferably, described data sending terminal also comprises transmission buffering area control unit, is used to described buffered data encapsulation unit before described packet to be sent is added into described transmission buffering area, judges whether described transmission buffering area fills up, if then abandon this packet to be sent.
Compared with prior art, the present invention has the following advantages:
In the preferred embodiment of the present invention scheme, only whether the data format of data transmitting terminal and process of transmitting and data receiver being acknowledged receipt of packet controls, need not to send the attended operation of shaking hands of special packet between data sending terminal and the data receiver, keeping under the situation that the udp protocol overhead is few, data transmission efficiency is higher than Transmission Control Protocol, solved being easy to of existing in the existing udp protocol transmission mechanism produce packet loss phenomenon and data packet disorder arrive phenomenon and cause can not reliable transmitting data problem.
In the further preferred embodiment of the present invention, data receiver need not each packet that receives is all sent response data packet, only the expected data bag is sent response data packet, after data sending terminal receives this response data packet, all transmission packets that will be not more than sequence number in (or being not less than) this response data packet are all from sending buffer block deletion, can further reduce both sides' interaction times and communication data amount, improve communication efficiency.
In the further preferred embodiment of the present invention, number of retransmissions by supervision packet is judged the network environment situation, and adjust the time of staying of packet successively automatically at buffering area, thereby, can further improve data transmission efficiency between data sending terminal and the data receiver according to the network environment situation.
Embodiment
For above-mentioned purpose of the present invention, feature and advantage can be become apparent more, the present invention is further detailed explanation below in conjunction with the drawings and specific embodiments.
Method embodiment one:
With reference to Fig. 1, show the flow process of method for reliable transmission first embodiment of data of the present invention, specifically may further comprise the steps:
Step S101: the file that data sending terminal will send splits into and is provided with the serial number data bag;
After data receiver is received all packets, can be reassembled into data file according to the sequence number of each packet.
Step S102: by default buffered data packet format the above-mentioned data envelope that is provided with sequence number is dressed up packet to be sent, and be added into the transmission buffering area;
In this default buffered data packet format, the data (data) that should comprise sequence number (Sq_no) at least and will transmit if the data length that will transmit is unfixing, also should comprise data length (len); In addition, judge and record data bag re-transmission situation, can also comprise timestamp (time) and number of retransmissions (times) for convenient.
For preventing that sending buffering area overflows, before packet to be sent being added into the transmission buffering area, should judge that also whether send buffering area also has clearance spaces, if it is full to send buffering area, can select to abandon this packet to be sent.
Step S103: the packet to be sent that will send in order in the buffering area sends;
In this preferred embodiment, send packet to be sent by sequence number order from small to large.
In addition,, data envelope to be sent is dressed up the transmission packet, data that this protocol header form comprises package identification symbol, data length, will transmit etc. by the protocol header form of mutual agreement sending before packet to be sent in the buffering area sends.
Step S104: deletion sends the packet that data receiver has been confirmed in the buffering area;
Being needs the reliability guarantee that data send data receiver to reply and confirms to reply, and has only and confirms locally to receive or confirms under the obstructed situation of network that the packet that data sending terminal just will send in the buffering area is deleted.In this preferred embodiment, for reducing both sides' interaction times and transinformation content, data receiver is only replied answer to the packet of the sequence number maximum that acknowledges receipt of, after data sending terminal is received this answer, all delete sending the packet that in the buffering area all are less than or equal to this sequence number.
Step S105: judge that whether the time of staying that sends packet in the buffering area is above default time threshold, if then change step S106; Otherwise, do not do any processing;
In this preferred embodiment, the time of staying threshold value of packet in sending buffering area at first is decided to be 500 milliseconds.When monitor thread finds that the time of staying of certain packet surpasses this threshold value, then it is retransmitted, and the record of increase number of retransmissions, when the accumulative total number of retransmissions of all retransmission data packet may reaches certain certain value (as 5 times), illustrate that network environment is relatively poor, can select increases a fixed value (as 1000 milliseconds) with time of staying threshold value, increase along with the accumulative total number of retransmissions, time of staying maximum also increases discretely, reach certain value (as 10 times) up to the packet that reaches maximum retransmission, it is obstructed then can to regard as network.When network environment preferably the time, the accumulative total number of retransmissions of retransmission data packet may reduces, and time of staying threshold value is also correspondingly discrete to be reduced, but minimum can not be less than 500 milliseconds.
Step S106: this packet is reset to packet to be sent, change step S103 and repeat above-mentioned process of transmitting;
When the number of retransmissions of a packet during greater than certain number of times (as 5 times), it is obstructed to regard as network, no longer sends packet, can select to send buffering area and empty; When receiving the respond packet of data receiver once more, can regard as network recovery, recover to send the process of packet again.
Method embodiment two:
In this preferred embodiment, communication two party is data sending terminal and data receiver each other, when sending data, can also receive the data from the other side, and communication two party adopts the common protocols head, and the form of protocol header is as shown in table 1:
The form of table 1, protocol header
?Begin_tag |
Cmd |
Sq_no |
?Ack_seq |
datalen |
data |
Wherein, Begin_tag has only the bag of this sign just to take bag to be processed as for the bag sign; Cmd is the command word of upper-layer protocol; The sequence number that Sq_no wraps for this reason; Ack_seq wraps the reply data packet number of opposite end when replying for this reason; Datalen wraps the data length that will transmit for this reason; Data wraps the data that will transmit for this reason.
Each data terminal is provided with and sends buffering area and receive buffering area, be convenient management, buffering area adopts the queue structure of going in ring, and the packet that each data terminal will send is put into the transmission buffering area, the data that receive are put into the reception buffering area, and the form of buffering area bag is as shown in table 2:
The form of table 2, buffering area bag
buffer |
len |
Sq_no |
time |
times |
Wherein, the data of buffer for transmitting; Len is a data length; Sq_no is packet number for this reason; The timestamp that time wraps for this reason; The number of retransmissions that times wraps for this reason.
In addition, this preferred embodiment is not only controlled the data process of transmitting, also data receiving course and acknowledgement mechanism thereof are improved, with from small to large sending order is example, when replying, the mode that each packet is all replied, be improved to the reply data bag of only replying the sequence number maximum, after data receiver is received this response packet, all packets that sequence number in the buffering area are not more than this response packet sequence number are all deleted, significantly reduce the network interaction data volume, improved data transmission efficiency.
With reference to Fig. 2-1, show the flow process of the method for reliable transmission second embodiment Data Receiving of data of the present invention, specifically may further comprise the steps:
Step S201: the file that will send splits into the packet that is composed of sequence number;
Step S202: judge whether the transmission buffering area full? if change step S203; Otherwise, change step S204;
Step S203: abandon this packet;
Step S204: after data encryption, data envelope is dressed up packet to be sent, and be added into the transmission buffering area by the buffering area packet format shown in the table 2;
In this preferred embodiment, before encapsulation of data, earlier data are encrypted; In specific implementation process, can select as required whether to encrypt.
Step S205: judge to send buffering area go in ring packet in the formation sequence number (Sq_no) whether≤sequence number (Ack_seq) that data receiver is confirmed? if change step S206; Otherwise, change step S207;
In this preferred embodiment, for guaranteeing data transmission efficiency, communication two party can add response message when sending packet, therefore, can further reduce interaction times, improve data transmission efficiency.
Step S206: this packet is deleted in formation from going in ring;
Step S207: send this packet to be sent;
Step S208: judge that this packet needs to retransmit? if change step S205; Otherwise, change step S206.
By the timestamp (time) and the current time in system of comparing data bag, the time of staying of finding certain packet is during the overtime threshold value, increase the value of number of retransmissions (times), and the timestamp (time) and the number of retransmissions parameters such as (times) of this packet reset, change judgement, the process of transmitting of step S205 repetitive data packet then.
In the specific implementation, can be by the transmit status of packet in dedicated process or the dedicated thread monitoring transmission buffering area, the transmit status of the packet in also can the cycle criterion circle queue, the present invention is not limited this.
With reference to Fig. 2-2, show the flow process of the method for reliable transmission second embodiment data transmission of data of the present invention, specifically may further comprise the steps:
Step S221: receive the packet that data sending terminal sends;
Step S222: judge that this packet is the packet of expectation? if change step S226; Otherwise, change step S223;
After data receiver receives packet, judge at first whether this reception sequence of data packet number is the expectation receive sequence number, if the sequence number of expectation will expect that then sequence number increases progressively, and handles this packet; Otherwise, this packet is added in the formation of reception buffering area.
Step S223: judge that this is the packet of repetition? if change step S224; Otherwise change step S225;
The sequence number of packet sequence number that receives and the packet that receives the buffering area preservation is compared judgement, if the packet (packet that has promptly received) that repeats then abandons it; If it is full to receive buffering area, then empty buffering area, this packet is handled, then, the expectation sequence number is provided with the next packet of packet for this reason.
Step S224: the packet that abandons this repetition;
Step S225: this packet is added the reception buffering area;
Step S226: to receiving data packet sequencing in the buffering area;
Whenever receive an expected data bag, all to check and receive the packet of having preserved in the buffering area, according to its sequence number it is sorted, from small to large, if just, expect that then sequence number increases progressively, and this packet handled for the expectation packet, from receive buffering area, this packet bag is deleted then, till non-expected data bag; Expect that sequence number has increased progressively and be continuous data bag maximum sequence number+1 that has received this moment, send ordering back maximum sequence number to data sending terminal and wrap in response.
Step S227: will expect that sequence number is incremented to the continuous data bag maximum sequence number that has received and adds 1;
Step S228: handle above-mentioned expected data bag, then from receiving buffer block deletion;
For treated packet, promptly have the packet of continuous sequence number, can be from receiving buffer block deletion, for the follow-up data bag provides spatial cache.
Need to prove that in the above preferred embodiment, data sending terminal is that to send packet in proper order with from small to large sequence number be that example describes.If data sending terminal sends packet in proper order with sequence number from big to small, then the processing mode of above-mentioned data receiver is just in time opposite, that is: if the packet that data receiver receives is the expectation packet, the packet that then will receive in the buffering area is pressed the sequence number ordering, and the minmal sequence that the expected data packet number changes the continuous packet of sequence number into number subtracted 1, and, by default protocol header form this minmal sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into the reception buffering area; Wherein, all packets more than or equal to sequence number in the response data packet are the packet that data receiver acknowledges receipt of.
In addition, when sending response data packet, can in sending packet, add this response message, thereby further reduce both sides' interaction times, raising communication efficiency according to the protocol header form of this preferred embodiment.
For aforesaid each method embodiment, simple in order to describe, so it all is expressed as a series of combination of actions, but those skilled in the art should know, the present invention is not subjected to the restriction of described sequence of movement, because according to the present invention, some step can adopt other orders or carry out simultaneously; Secondly, those skilled in the art should know that also said method embodiment all belongs to preferred embodiment, and related action and module might not be that the present invention is necessary.
Device embodiment:
With reference to Fig. 3, show the structured flowchart of reliable transmission device one embodiment of data of the present invention, comprise data sending terminal 31, data receiver 32 and network 33, data sending terminal 31 is connected by network 33 with data receiver 32, data sending terminal 31 specifically comprises data split cells 311, buffered data packet encapsulation unit 312, data transmission unit 313, protocol package encapsulation unit 314, response confirmation unit 315, packet reset cell 316, parameter adjustment unit 317, send buffering area control unit 318 and send buffering area 310, data receiver 32 specifically comprises reception buffering area 320 and Data Receiving processing unit 321, wherein:
The data file that data split cells 311 is used for sending splits into several packets, and for each packet is provided with a sequence number, organizes the foundation of bag spanned file again as data receiver 32;
Buffered data encapsulation unit 312 is used for by default buffered data packet format the data envelope that disposes sequence number that data split cells 311 generates being dressed up packet to be sent, and is added into transmission buffering area 310;
Send buffering area control unit 318 and be used to buffered data encapsulation unit 312 before packet to be sent being added into transmission buffering area 310, judge whether send buffering area 310 fills up, if then abandon this packet to be sent;
The packet to be sent that data transmission unit 313 is used for will sending in order buffering area 310 is sent to data receiver 32 by network 33;
Protocol package encapsulation unit 314 is used to data transmission unit 313 to dress up the transmission packet by the data envelope to be sent that default protocol header form will send in the buffering area 310;
The packet that response confirmation unit 315 is used for data receiver 32 has been confirmed is from sending buffering area 310 deletions;
Packet reset cell 316 is used for judging that whether the time of staying that sends buffering area 310 packets is above default time threshold, if then this packet is reset to packet to be sent;
Parameter adjustment unit 317 is used for writing down its number of retransmissions when the packet that packet reset cell 316 will need to retransmit resets to packet to be sent; And, judge that whether the number of retransmissions that sends retransmission data packet may in the buffering area 310 is all greater than default number of retransmissions higher limit, if then increase a fixed value for default time threshold; And judge that whether the number of retransmissions that sends retransmission data packet may in the buffering area 310 is all less than default number of retransmissions lower limit, if then reduce by a fixed value for default time threshold;
Send buffering area 310 and be used to preserve the packet to be sent that buffered data packet encapsulation unit 312 generates, and the packet that data transmission unit 313 has sent, response confirmation unit 315 does not also have deletion.
Data Receiving processing unit 321 is used for whether the packet that judgment data receiving terminal 32 receives is the expectation packet, if, the packet that then will receive in the buffering area 320 is pressed the sequence number ordering, and the maximum sequence number that the expected data packet number changes the continuous packet of sequence number into added 1, and, by default protocol header form this maximum sequence number is packaged into response data packet and sends; Otherwise, this packet that receives is put into reception buffering area 320; Wherein, all are less than or equal to that the packet of sequence number is the packet that data receiver 32 acknowledges receipt of in the response data packet.
Need to prove that said apparatus embodiment belongs to preferred embodiment, related unit and module might not be that the present invention is necessary.
Each embodiment in this specification all adopts the mode of going forward one by one to describe, and what each embodiment stressed all is and the difference of other embodiment that identical similar part is mutually referring to getting final product between each embodiment.For device embodiment of the present invention, because it is similar substantially to method embodiment, so description is fairly simple, relevant part gets final product referring to the part explanation of method embodiment.
More than the method for reliable transmission and the device of a kind of data provided by the present invention is described in detail, used specific case herein principle of the present invention and execution mode are set forth, the explanation of above embodiment just is used for helping to understand method of the present invention and core concept thereof; Simultaneously, for one of ordinary skill in the art, according to thought of the present invention, the part that all can change in specific embodiments and applications, in sum, this description should not be construed as limitation of the present invention.