- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Delphi 中使用 TIdTCPClient/TIdTcpServer indy 组件编写客户端-服务器应用程序。
数据传输通常工作正常,但我经常从服务器读取错误的数据;我得到了以前请求的答案,而不是当前的答案。
在调试期间,两个应用程序都在本地工作,因此传输过程中数据不会丢失。
超时为 1000-3000 毫秒,这足以避免在收到第一个请求的答复之前发送第二个请求。
我使用简单的数据格式:前4个字节是数据包长度,其余是该长度的二进制数据。
服务器端代码是(简化为仅发送字符串;我也以同样的方式使用二进制缓冲区,但此代码更易于理解和检查):
Var
lng: LongInt;
ib: TIdBytes;
begin
// Prepare data to send:
lng:=length(s);// s is an AnsiString to be sent
SetLength(ib,lng+4);
Move(lng,ib[0],4);
Move(s[1],ib[4],length(s));
// Send:
AContext.Connection.IOHandler.WriteDirect(ib);
end;
发送请求的客户端代码是相同的(在最后一行调用 TIdTcpClient.IOHandler.WriteDirect())。读取服务器答案的客户端代码是:
Var
ib: TIdBytes;
size,done,lng: LongInt;
begin
Result:=false;
// answer length:
try
SetLength(ib,0);
tcp.IOHandler.ReadBytes(ib,4,false);
Move(ib[0],size,4);
if length(ib)<0 then Exit;// wrong data
except
on E: Exception do;// code skipped
end;
// read answer body:
done:=0;
b.Clear;// b is my buffer, TStream descendant
while done<size do
begin
lng:=Min(size-done,MaxBlockSize);
// read:
SetLength(ib,0);// to be sure
tcp.IOHandler.ReadBytes(ib,lng,false);
if length(ib)=0 then Exit;// error reading
// append my buffer:
b.Wr(ib[0],length(ib));
// progress:
Inc(done,length(ib));
end;
end;
数据交换顺序为:
客户端向服务器发送请求,
服务器读取请求并将答案发送回客户端,
客户读取答案。
第 3 步出现错误数据。
也许我做的事情总体上是错误的?
在向服务器发送请求以清除传入缓冲区之前,我尝试过 ReadBytes(),但这也没有帮助,就像我尝试过的许多其他操作一样......
现在我没有主意了:(
最佳答案
您的 I/O 逻辑比需要的复杂得多,尤其是在客户端。您正在手动执行 Indy 可以自动为您执行的操作。
在客户端,由于您将数据保存到 TStream 中,因此您可以让 Indy 直接将数据读取到 TStream 中:
begin
...
b.Clear;// b is my buffer, TStream descendant
// ReadStream() can read a '<length><bytes>' formatted
// message. When its ASize parameter is -1 and its
// AReadUntilDisconnect parameter is False, it reads
// the first 4 or 8 bytes (depending on the LargeStream
// property) and interprets them as the byte count,
// in network byte order...
tcp.IOHandler.RecvBufferSize := MaxBlockSize;
tcp.IOHandler.LargeStream := False; // read 4-byte length
// read answer:
try
tcp.IOHandler.ReadStream(b, -1, false);
except
on E: Exception do begin
// the socket is now in an indeterminate state.
// You do not know where the reading left off.
// The only sensible thing to do is disconnect
// and reconnect...
tcp.Disconnect;
...
end;
end;
...
end;
在服务器端,您可以通过两种不同的方式发送与上述代码兼容的消息:
var
lng: LongInt;
ib: TIdBytes;
begin
// Prepare data to send:
// s is an AnsiString to be sent
lng := Length(s);
SetLength(ib, lng);
Move(PAnsiChar(s)^, PByte(ib)^, lng);
// Send:
AContext.Connection.IOHandler.Write(lng); // send 4-byte length, in network byte order
AContext.Connection.IOHandler.Write(ib); // send bytes
end;
或者:
var
strm: TIdMemoryBufferStream;
begin
// Prepare data to send:
// s is an AnsiString to be sent
strm := TIdMemoryBufferStream.Create(PAnsiChar(s), Length(s));
try
// Send:
// Write(TStream) can send a '<length><bytes>' formatted
// message. When its ASize parameter is 0, it sends the
// entire stream, and when its AWriteByteCount parameter
// is True, it first sends the byte count as 4 or 8 bytes
// (depending on the LargeStream property), in network
// byte order...
AContext.Connection.IOHandler.LargeStream := False; // send 4-byte lengtb
AContext.Connection.IOHandler.Write(strm, 0, True);
finally
strm.Free;
end;
end;
如您所见,此代码发送的消息类型与您最初发送的消息类型相同,更改的是管理消息的代码。此外,它还强制消息字节计数以网络字节顺序发送,而您是以主机字节顺序发送消息。多字节整数应尽可能以网络字节顺序发送,以实现一致性和多平台兼容性。
关于delphi - Delphi 使用 TIdTCPServer/TIdTCPClient 组件丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27984142/
我正在使用 TidTCPServer 和 TidTCPClient 在 delphi 中开发消息系统。 首先,所有客户端都使用用户名和密码登录。服务器使用ADO组件在SQL Server中搜索用户数据
我在 TIdTCPServer(安装时附带的 Delphi 2009 和 Indy 10)的 OnExecute 中有以下代码,这与本网站上的其他示例非常相似; Socket := AConte
我正在尝试断开连接到 TIdTCPServer 的非事件客户端的连接,无论这些客户端是与 Internet 断开连接还是在一段非事件时间内。 我尝试在 OnConnect 事件中设置超时,如下所示:
始终建议在 OnExecute 中执行所有发送/接收任务TIdTCPServer 的事件处理程序,但我不明白以下内容: 如何等待输入的特定序列并同时将一些数据发送到同一个客户端?我不需要命令响应序列,
我有一个使用 Delphi 10.1 创建的 64 位服务器应用程序,在这个应用程序中我有一个 TIdTCPServer 组件。 当客户端连接时,线程计数会增加,而当客户端断开连接时,线程计数不会减少
我需要编写一个 Delphi 2009 应用程序,它从套接字读取数据。为此,我需要为 TIdTCPServer.OnExecute 事件编写一个事件处理程序。 我发现了很多在 GUI 应用程序中实现此
我有一个使用 TIdTCPServer 的服务器程序和一个客户端程序。我在一台计算机上运行我的客户端程序,例如 3 次。每次连接客户端时,我都会尝试向备忘录中添加一些内容。这就是问题所在。由于 3 个
我们有一个应用程序使用 Delphi 2007 附带的 Indy 10.1.1 组件监听传入的 TCP 请求。 有时我们会收到不是来自客户端应用程序的传入连接。通常,会发生以下两种情况之一:1) 在接
我有一个 TidTCPServer,它在 onExcecute 事件中使用数据库操作(通过使用 TidNotify)。一切都很好,而不是可能关闭应用程序。 在关闭应用程序期间,我不知道所有通知实例是否
我想知道当前到 Indy 9 TIdTCPServer 的客户端连接数(在 Delphi 2007 上) 我似乎找不到提供此功能的属性。 我尝试在服务器 OnConnect/OnDisconnect
我需要帮助来了解如何通过 Indy TCP 服务器/客户端传输记录。我有两个程序,一个是客户端,另一个是服务器。在客户端上的按钮上我放置了连接:客户端是 TIdTCPClient Client.Con
我创建了 TIdTcpServer 的不同实例,并为稍后客户端连接/断开/执行时需要的 Data 属性分配了一个指针。 是否可以从 TIdContext 获取“父级”TIdTcpServer? 如果是
我是 Indy 的新手。这是我第一次在这里发布问题。 我的项目必须以 60Hz 的频率向所有客户端发送数据。为此,我正在使用 TIdTCPServer 来监控保持事件状态。我的工具很旧,在 WinXP
我正在 Delphi 中使用 TIdTCPClient/TIdTcpServer indy 组件编写客户端-服务器应用程序。 数据传输通常工作正常,但我经常从服务器读取错误的数据;我得到了以前请求的答
我正在使用 Delphi xe6 来实现一个简单的客户端/服务器连接。客户端表单应该有一个 TEdit 组件,并且应该将 Edit.text 字符串发送到服务器备忘录。我想使用 Indy 组件:TId
我知道我希望从客户端获得多少字节(在此代码中为 100)。如果客户端不给我 100,我根本不需要这个数据包,因为它已损坏。 我遇到客户端提供少于 100 个字节并断开连接的情况。在这种情况下,我正在捕
我有一个简单的 TidTCPServer 在控制台上工作并接受数据。我的问题是当客户端发送流但交换数据的速度非常快时,服务器在 70 行后卡住,服务器的 CPU 负载达到 70%;我不知道如何在每次发
我正在尝试一些 IP PBX 系统(Asterisk、Freeswitch 和 Yate)来注册 PBX 中的事件,我想知道这些组件中哪一个更好。 该组件应该向 PBX 注册事件、接收事件、发送一些响
我正在摆弄 Delphi 2009 提供的 Indy 10,并且在 OnExecute 触发时无法从 IOHandler 获取所有数据... procedure TFormMain.IdTCPServ
如果我有一个 TidTCPServer实例,我声明一个 TFormatSettings并将其填充到 Create 中例程,引用这个变量是否安全(例如,当可能有多个上下文执行时,在线程的 Format
我是一名优秀的程序员,十分优秀!