var
connection: TclTcpClientConnection;
data: TStream;
bytes: TBytes;
dataSize: Integer;
begin
StartupSocket(); //initialize WinSock
connection := TclTcpClientConnection.Create();
data := TMemoryStream.Create();
try
connection.NetworkStream := TclNetworkStream.Create();
connection.Open('192.168.1.1', 12345); //open the connection
connection.ReadData(data); //read two bytes with the size information
data.Seek(0, soBeginning);
SetLength(bytes, 2);
data.Read(bytes, 2);
dataSize := bytes[1] + bytes[0] shl 8; //decode the data size
data.Seek(0, soEnd); //move to the end of the stream and read the rest of data
while (data.Size < dataSize + 2) do //two bytes with the size information are not included in data
begin
connection.ReadData(data);
end;
data.Seek(2, soBeginning); //skip two bytes with the size before extracting data
SetLength(bytes, dataSize);
data.Read(bytes, dataSize); //the received bytes
finally
data.Free();
connection.Free();
end;
CleanupSocket(); //deinitialize WinSock
end;
Article ID: 96, Created: December 27, 2017 at 1:15 PM, Modified: November 20, 2019 at 11:33 PM