Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell the TCP server that the particular message has ended?

TCP client sends data byte by byte. So, how to tell the server that this message has ended and the new message begins now?

One way is to fix a special character that'll be sent as a bookmark, but that character can also be a part of the message causing confusions.

Any other optimum way out?

like image 591
Aquarius_Girl Avatar asked Nov 17 '25 06:11

Aquarius_Girl


2 Answers

If the message is binary, delimited encoding using a special character is not possible. Tag Length Value (TLV) encoding will be best suited for this.

for example

 +--------+----------+----------------+    
 |  Tag   | Length   |  Content       |    
 | 0x0001 |  0x000C  | "HELLO, WORLD" |    
 +--------+----------+----------------+

in addition to that, you can have more than one message type

like image 91
Jestan Nirojan Avatar answered Nov 20 '25 04:11

Jestan Nirojan


One possible way can be that before sending the actual message you can send the number of bytes in the particular message. When the receiving side has received that number of bytes it can start receiving next message

like image 38
MARK Avatar answered Nov 20 '25 04:11

MARK