I'm using Delphi XE8 for developing mobile apps and Desktop application. In mobile application, I'm using TIDtcpClient component and in Desktop application application, I'm using TServerSocket.
Server Desktop application contains TList which contains some 1500 records. For getting these values in Client Mobile application, I'm using the following method.
- First the download request is send from Client mobile app to Server application.
- Next it retrieves 10 records and sends back to Client mobile application. After this it updates the values in Client List and then again it sends back the request to server app.
- Till the record count reaches, this process continues.
The problem is when I'm following this method it takes almost 2 minutes of time and I'm getting all the data properly. So I have decided to use the file stream method. Below I have mentioned the sample code:
Server Side app:
//first saved the List into FileStream & it is working as I have reloaded and checked
//So again I'm loading the saved file, The file Size is near to 400KB
FileStream := TFileStream.Create('D:\ListDet.dat', mtfmOpenRead);
Socket.SendStream(FileStream);
Client Side mobile app:
var
FileS: TFileStread;
i: Size;
begin
//Inside the thread
TiDTcpClient.IOHandler.ReadStream(FileS, i);
end;
And When I'm using the above method, I'm getting the exception and I'm can not retrieve data.
Please provide me any solution to retrieve the data faster from Server to Client.
You did not show/explain what actual I/O methods you were using to send the data in that situation.
TCustomWinSocket.SendStream()
simply sends the content of theTStream
as-is, it does not send anything else.You are passing an uninitialized variable,
i
, toTIdIOHandler.ReadStream()
. That parameter tellsReadStream()
how many bytes to read. Sincei
is uninitialized, its value is whatever random data happens to be on the stack at that moment.If
i
happens to be> 0
at runtime, that is how many bytesReadStream()
will try to read. If that many bytes are not actually sent,ReadStream()
will block the calling thread waiting for more bytes until itsReadTimeout
elapses (which is infinite by default) or the socket is disconnected.If
i
happens to be exactly-1
(and theAReadUntilDisconnect
parameter is False by default),ReadStream()
will try to read anInteger
/Int64
(depending on theTIdIOHandler.LargeStream
property) from the socket and use that as the byte count to finish reading the rest of the stream.TCustomWinSocket.SendStream()
is not sending any such size value.If
i
happens to be< 0
,ReadStream()
will simply ignore the byte count and just keep reading until the socket is disconnected (AReadUntilDisconnect
is forced to True).The default behavior of
TIdIOHandler.ReadStream()
is to expect the stream data to be preceded by the stream size, but you are overriding that behavior by passing the uninitializedi
variable, so the behavior is undefined.Your server is not sending the stream size before sending the stream data, so the client has no way of knowing how many bytes to expect, unless you disconnect the socket after sending the stream.