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,

CodePudding user response:

refer to the second floor wangyang4506 response:
can't 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);




Equipment return packets is a deal, I think you said this is supposed to be more close to my reality, the whole package receiving, can I use the recv function, where not to determine the length of the line? Can be under the guidance of it in detail, I really too in this dish

CodePudding user response:

Leng:=Socket. ReceiveLength; How does this take? Don't

CodePudding user response:

reference BDMH reply: 3/f
is the subcontract, if u can add 4 bytes to store data before sending the data length, when received, first to read the first 4 bytes, length, and then according to the length to the back of the data

LZ protocol description is not detailed, but the webmaster can solve your problem

CodePudding user response:

refer to 6th floor u012372620 response:
leng:=Socket. ReceiveLength; How does this take? Don't


I do TCP communications generally use is bringing the ClientSocket and ServerSocket, ClientSocket inside a OnRead events
Procedure TMainForm. ClientSocketRead (Sender: TObject; Sockets: TCustomWinSocket);
Data reception, Socket ReceiveLength is triggered after the access to the packet length,
You said the recv I don't know is which to come in,

CodePudding user response:

refer to the eighth floor wangyang4506 response:
Quote: refer to the sixth floor u012372620 response:

Leng:=Socket. ReceiveLength; How does this take? Don't


I do TCP communications generally use is bringing the ClientSocket and ServerSocket, ClientSocket inside a OnRead events
Procedure TMainForm. ClientSocketRead (Sender: TObject; Sockets: TCustomWinSocket);
Data reception, Socket ReceiveLength is triggered after the access to the packet length,
Recv you said I don't know what is inside,


Turned out to be so, I didn't use control, establish the condom section directly

CodePudding user response:

refer to 7th floor twtynk1 response:
Quote: reference BDMH reply: 3/f

Is the subcontract, if u can add 4 bytes to store data before sending the data length, when received, first to read the first 4 bytes, length, and then according to the length to the back of the data

LZ protocol description is not detailed, but the webmaster can solve your problem


The others said before, but look at my agreement don't feel real,,,,

The following is my agreement
Response frame structure:
+ + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
Bytes | | | field description |
+ + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
| | 1 | 0 x02 frame head, marks the beginning of a frame for the receiver to provide synchronization |
| address | 2 | variable message sign, are set in the variable message sign manufacturers |
| | frame data u | |
| frame check | 2 | for the receiver to judge the correctness of the received frame |
| | 1 | 0 x03 frame end, which indicates that the end of a frame, provide the receiver with the synchronous |
+ + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +

CodePudding user response:

0 x03, indicating the end of a frame?
The frame data appeared in the 0 x03?

CodePudding user response:

Appeared end logo should be escaped in the data transmission

The
reference 11 floor sz_haitao response:
0 x03, indicating the end of a frame?
The frame data appeared in the 0 x03?

CodePudding user response:

A custom package structure, including content length and data line!

CodePudding user response:

Don't give some case,,,,

CodePudding user response:

One receives no problems, unless the host's equipment is too much,

CodePudding user response:

refer to 6th floor u012372620 response:
leng:=Socket. ReceiveLength; How does this take? Don't

So when you define protocol frame head (fixed length, and a frame head logo, account ID number, continuous transmission to sequence may not be the same, is convenient to receive after the completion of the set of bag and then parse) to include the length of the data, to tell what's the length of the next frame data needs to be read, read after the data volume, end frame (fixed length, and end frame), the information contained in general have the check digitnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related