Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi client server array sending

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)?

like image 489
DanilGholtsman Avatar asked Nov 21 '12 14:11

DanilGholtsman


1 Answers

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.

like image 99
Arnaud Bouchez Avatar answered Oct 06 '22 00:10

Arnaud Bouchez