gpt4 book ai didi

Delphi — TClientSocket ReceiveText MaxLen?

转载 作者:行者123 更新时间:2023-12-03 15:57:12 24 4
gpt4 key购买 nike

我一直在尝试设置从 ReceiveText TClientSocket 函数恢复的字符数量的长度,但似乎没有任何效果。例如,从恢复的数据或其他数据流接收第一个最左边的字符。有没有办法在 Delphi 中使用这个特定对象来完成这个任务?

非常感谢您的帮助。提前致谢。

最佳答案

ReceiveText 没有任何方法来控制接收文本的最大长度。

ClientType := ctBlocking 模式中最简单的方法是使用 TWinSocketStream,如文档所述:

http://docwiki.embarcadero.com/VCL/XE2/en/ScktComp.TClientSocket.ClientType

When ClientType is ctBlocking, use a TWinSocketStream object for reading and writing. TWinSocketStream prevents the application from hanging indefinitely if a problem occurs while reading or writing. It also can wait for the socket connection to indicate its readiness for reading.

示例代码:

var
Stream : TWinSocketStream;
Buffer : TBytes;
S : string;
begin
SetLength(Buffer, 100); // 100 bytes buffer size
Stream := TWinSocketStream.Create(Socket, 5000); // 5 seconds or 5000 milliseconds
try
Stream.ReadBuffer(Buffer[0], Length(Buffer)); // raises an Exception if it couldn't read the number of bytes requested
S := TEncoding.Default.GetString(Buffer); // Works in Delphi 2009+
finally
Stream.Free;
end;
end;

关于Delphi — TClientSocket ReceiveText MaxLen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9188685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com