6

I need to send a dynamic array of objects from server to clients. An example as array which cointains objects of class Figure with information about its coordiantes, color etc.

I was trying to use something like this to send data:

for i := 0 to ServerSocket.Socket.ActiveConnections - 1 do
begin
  ServerSocket.Socket.Connections[i].SendText(some values); // then parsing this text to get values
end;

but it's really bad way to do like that I think. So, please, could you show me the right way of how to send and recieve array of some objects (using IdTCPClient/Server or Client/ServerSockets)?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69
  • What platform/language is the client running on? Is that Delphi too? – Steve Mayne Nov 21 '12 at 15:58
  • 1
    @SteveMayne yep, it's Delphi too. – DanilGholtsman Nov 21 '12 at 16:00
  • There's no standard way to pickle/marshall objects in Delphi (as far as I'm aware). You could look into using SOAP to do this communication: http://stackoverflow.com/questions/6415437/soap-server-and-client-application-vclindy-demo-for-delphi-xe – Steve Mayne Nov 21 '12 at 16:18
  • 2
    @Steve, there is [`JSON marshaling`](http://docwiki.embarcadero.com/RADStudio/XE3/en/Serializing_User_Objects) available in Delphi, but not yet in Delphi 7. – TLama Nov 21 '12 at 16:23
  • @TLama there are free and open source libraries (for example lkJSON and SuperObject) for JSON serialization, which support older Delphi versions – mjn Nov 21 '12 at 17:26
  • @mjn, I know, Steve's comment just sounds to me like *there's no built-in marshaling in Delphi (at all)*. But anyway, are you sure that SuperObject supports marshaling for Delphi without extended RTTI (below 2010) ? I wouldn't say so... About lkJSON, I don't know it, but from a very quick look through the source code, there's no mention of RTTI at all, so here I'm not sure as well. – TLama Nov 21 '12 at 17:47
  • @TLama btw, can I ask you about one thing? I read some examples and there are always saving to file stuff and then reading from it. Is it always like that or I can some way work with streams without saving in files? P.S. sry for my english – DanilGholtsman Nov 21 '12 at 18:01
  • 2
    @Danil, there's no need to store those serialized data to file at all. It's definitely fine and much more efficient to store them in memory (simply in some variable or in some sophisticated buffer). – TLama Nov 21 '12 at 18:08
  • @TLama oh, could you help me to find such examples, please? – DanilGholtsman Nov 21 '12 at 18:13

1 Answers1

4

You must serialize your dynamic arrays between clients and server.

First attempt is to use your own serialization, e.g. using a #13#10 (CRLF) delimited content if this is an array of string, or a binary serialization otherwize.

Under Delphi 7 (and up) you can use our dynamic array wrapper, which enables serialization into JSON or binary. It will do everything directly, using available RTTI. It will work will all basic types: integers, floating-points, strings, nested arrays, but not class instances.

For a dynamic array of objects (class instances), you will need to make the serialization by hand. Using TReader may help, or you have to code it yourself. It could be a good idea to serialize all items on each side into one stream, then send the content at once, for better performance.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • 1
    So there's is no way I could do it directly? Oh. And I wish I could use your wrapper but I not able to use not native/not mine delphi libraries to make my task, sorry. – DanilGholtsman Nov 21 '12 at 16:23
  • 2
    @Danil, then you're out of luck, since marshaling was introduced in Delphi 2010 [+1ed]. – TLama Nov 21 '12 at 16:31
  • 1
    @TLama well, it's okay. However I think I need to learn howto serialaize stuff! – DanilGholtsman Nov 21 '12 at 16:35
  • 1
    Even newer versions of Delphi won't be able to marshall an array of objects, unless each class has its own serializer. You can do the same with Delphi 7, with some classes like the one included in mORMot, our other librairies. – Arnaud Bouchez Nov 21 '12 at 18:40