Home > Back-end > Socket to receive variable length data packets
Socket to receive variable length data packets
Time:10-08
Of communication are not familiar with, in writing this recently, some problems with socket, Is on the receiving device end data, I plan to use the recv function but my packet length, and some may be very long, also the subcontract, but can you tell me how to achieve,
Had better be a one byte received,
I receive bag also has a law met with 03 02 to receive proof bag end,
Please guide!!!!! Little women appreciate,
CodePudding user response:
Generally starts a byte of the party awarding the contract, explain how long is the following content!
CodePudding user response:
Can not use A one byte accept it, you can define A communication protocol, A special data you start in baotou, such as two bytes 0 x01 0 xac, then you can keep up with the type of data package marks, such as 0 x01 represents the attribute of A 0 x02 B attributes plus the length of the packet data, also can use two bytes for instance 255 0 x00 0 XFF to keep up with the data bytes can finally be on the safe side and CRC check, ensure that the data is good, The command byte is 01 ac 01 00 ff + data + CRC so as not to is wrong, no one receives, can press a whole package receiving, general upper machine and lower machine is according to the similar protocol communications,
Part of the receiving code:
Var Leng: longword; Rbuf: an array of byte; The begin
Leng:=Socket. ReceiveLength; Setlength (rbuf, leng); For I:=low (rbuf) to high (rbuf) do The begin Rbuf [I] :=0 end;
Socket. ReceiveBuf (rbuf [0], leng);
CodePudding user response:
Is the subcontract, if u can add 4 bytes before data to send the length of the data storage, receiving, first to read the first 4 bytes, length, and then according to the length to the back of the data
CodePudding user response:
If the variable length of package should may be long in the front set a data packet length,