Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can know length of readstring in Idtcpserver
#3
(08-27-2021, 04:25 PM)rlebeau Wrote:
(08-27-2021, 05:50 AM)Zsanvs Wrote: How can read these strings by idtcpserver?

Since the strings are not fixed length, ReadString() is not appropriate for this task, unless the client sends the string length before the string data, which does not appear to be the case in your example.

Are the JSON messages delimited, such as with a line break?  If so, then you can use ReadLn() instead.

If not, then the best way to handle this would be to use a JSON engine that supports push parsing.  Then you can read arbitrary amounts of raw bytes from the socket, such as with ReadBytes(-1), and then push those bytes into the engine, and let it spit out complete JSON messages as they become available.

If that is not an option, then you will just have to buffer the bytes yourself as it arrives, and then parse only complete messages in the buffer.  For instance, in this case, each JSON message starts with a {, so keep buffering until the closing } is received (keeping track of nested {} pairs in the middle), and then parse the buffer up to that closing }, leaving any remaining bytes in the buffer for subsequent parsing of the next message.

It is really difficult to give you a definitive answer without knowing the exact protocol your IOT device is actually using on the wire.


Thank you for your reply.
I found each JSON message start with {"devId":"492C201010012591"  ,is it possible to split message by use ReadLn() ?
or as you mentioned another option buffer function, please give me some coding for your solution if it's possible . Smile it's hard for me to find some demo online.

Thank you for your help.
Reply


Messages In This Thread
RE: How can know length of readstring in Idtcpserver - by Zsanvs - 08-30-2021, 01:32 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)