简体   繁体   English

需要将数据从TServersocket发送到TidTcpClient

[英]Need to send data from TServersocket to TidTcpClient

I'm using Delphi XE8 for developing mobile apps and Desktop application. 我正在使用Delphi XE8开发移动应用程序和桌面应用程序。 In mobile application, I'm using TIDtcpClient component and in Desktop application application, I'm using TServerSocket. 在移动应用程序中,我使用的是TIDtcpClient组件,在桌面应用程序中,我使用的是TServerSocket。

Server Desktop application contains TList which contains some 1500 records. Server Desktop应用程序包含TList,其中包含约1500条记录。 For getting these values in Client Mobile application, I'm using the following method. 为了在Client Mobile应用程序中获取这些值,我使用以下方法。

  1. First the download request is send from Client mobile app to Server application. 首先,下载请求从客户端移动应用程序发送到服务器应用程序。
  2. Next it retrieves 10 records and sends back to Client mobile application. 接下来,它检索10条记录并将其发送回客户端移动应用程序。 After this it updates the values in Client List and then again it sends back the request to server app. 此后,它将更新“客户端列表”中的值,然后再次将请求发送回服务器应用程序。
  3. 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. 问题是,当我遵循此方法时,将花费近2分钟的时间,并且我会正确地获取所有数据。 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. 请提供任何解决方案,以更快地从服务器到客户端检索数据。

The problem is when I'm following this method it takes almost 2 minutes of time and I'm getting all the data properly. 问题是,当我遵循此方法时,将花费近2分钟的时间,并且我会正确地获取所有数据。

You did not show/explain what actual I/O methods you were using to send the data in that situation. 您没有显示/解释在那种情况下发送数据所使用的实际I / O方法。

So I have decided to use the file stream method ... And When I'm using the above method, I'm getting [Closed gracefully exception in Client app (Mobile)] and I'm can not retrieve data 因此,我决定使用文件流方法...当我使用上述方法时,我得到了[客户端应用程序(移动)中的“优雅关闭异常”),我无法检索数据

TCustomWinSocket.SendStream() simply sends the content of the TStream as-is, it does not send anything else. TCustomWinSocket.SendStream()仅按TStream发送TCustomWinSocket.SendStream()的内容,不发送其他任何内容。

You are passing an uninitialized variable , i , to TIdIOHandler.ReadStream() . 您正在将未初始化的变量 i传递给TIdIOHandler.ReadStream() That parameter tells ReadStream() how many bytes to read. 该参数告诉ReadStream()要读取多少字节。 Since i is uninitialized, its value is whatever random data happens to be on the stack at that moment. 由于i未初始化,因此它的值就是那时随机数据在堆栈上的大小。

If i happens to be > 0 at runtime, that is how many bytes ReadStream() will try to read. 如果i在运行时碰巧> 0 ,那么ReadStream()将尝试读取多少字节。 If that many bytes are not actually sent, ReadStream() will block the calling thread waiting for more bytes until its ReadTimeout elapses (which is infinite by default) or the socket is disconnected. 如果没有实际发送那么多字节, ReadStream()将阻止调用线程等待更多字节,直到其ReadTimeout过去(默认为无限)或套接字断开连接为止。

If i happens to be exactly -1 (and the AReadUntilDisconnect parameter is False by default), ReadStream() will try to read an Integer / Int64 (depending on the TIdIOHandler.LargeStream property) from the socket and use that as the byte count to finish reading the rest of the stream. 如果i恰好是-1 (默认情况下AReadUntilDisconnect参数为False),则ReadStream()将尝试从套接字读取Integer / Int64 (取决于TIdIOHandler.LargeStream属性),并将其用作字节计数完成阅读流的其余部分。 TCustomWinSocket.SendStream() is not sending any such size value. TCustomWinSocket.SendStream()未发送任何此类大小值。

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). 如果i碰巧是< 0ReadStream()只会忽略字节数,只是继续读取直到套接字断开连接( AReadUntilDisconnect被强制为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 uninitialized i variable, so the behavior is undefined. TIdIOHandler.ReadStream()的默认行为是期望流数据之前带有流大小,但是您通过传递未初始化的i变量来覆盖该行为,因此该行为是不确定的。

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. 您的服务器没有在发送流数据之前发送流大小,因此,除非您在发送流之后断开套接字的连接,否则客户端无法知道需要多少字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM