question

Yue Y avatar image
0 Likes"
Yue Y asked Jacob Gillespie edited

clientreceive() read a integer

I want to use clientreceive() to read one integer sent from the server. Is it possible to read four bytes from the socket and convert it to an integer in FlexScript?

FlexSim 19.1.1
flexscript
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Mischa Spelt avatar image
1 Like"
Mischa Spelt answered Jacob Gillespie edited

Unfortunately clientreceive() only returns strings and FlexSim's string type is not so UTF-8 compliant that you can use that to transmit any integer.

I have written a module a while back that can transmit and receive binary data but it was for 17.0, I think, so it needs an upgrade here and there. Let me know by email if you are interested.

· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jacob Gillespie avatar image Jacob Gillespie ♦ commented ·

@Yue Y As Mischa said the FlexScript command cannot do this. If you use the c++ version of the command from a module then you could convert the bytes into an integer.

0 Likes 0 ·
Yue Y avatar image Yue Y Jacob Gillespie ♦ commented ·

thank you! How can I use c++ version in the Script? Do I just click the "C" button? In this mode, how can I convert the bytes into an integer, is there any function I can use?

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·

To use the c++ version I recommend using the module SKD. Otherwise if you put c++ into the model then you will have to compile it every time.

0 Likes 0 ·
Yue Y avatar image Yue Y Jacob Gillespie ♦ commented ·

I want to read a long message using clientreceive but I don't know the length of the message ahead of time. So that the server would send an integer first, indicating the length of the message. Then FlexSim should read the integer first and then read these amount of characters from the socket. Is it possible? Thank you!

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·

You could send the integer as a string padded to a certain length. Then remove the padding and parse the string as an integer in FlexScript. That way you wouldn't have to use c++.

0 Likes 0 ·
Yue Y avatar image Yue Y Jacob Gillespie ♦ commented ·

Also, for the FlexScript version of clientreceive, if the server sends a bytes while the buffer size is b bytes and a <= b, will FlexScript guarantee to read in all a bytes?

0 Likes 0 ·