【发布时间】:2021-05-12 08:52:27
【问题描述】:
我正在尝试使用 Indy 10 在两个 delphi 2010 应用程序之间发送字节,但没有成功。接收的字节与发送的字节不同。这是我的示例代码:
应用程序 1,发送按钮点击:
var s:TIdBytes;
begin
setlength(s,3);
s[0]:=48;
s[1]:=227;
s[2]:=0;
IdTCPClient.IOHandler.WriteDirect(s); // or .Write(s);
end;
应用 2,idtcpserver 执行事件(第一次尝试):
procedure TForm1.IdTCPServerExecute(AContext: TIdContext);
var rec:TIdBytes;
b:byte;
begin
SetLength(rec,0);
b:=AContext.Connection.IOHandler.ReadByte;
while b<>0 do
begin
setlength(rec, length(rec)+1);
rec[length(rec)-1]:=b;
b:=AContext.Connection.IOHandler.ReadByte;
end;
// Here I expect rec[0] = 48, rec[1]=227, rec[2]=0.
// But I get: rec[0] = 48, rec[1]=63, rec[2]=0. Rec[1] is incorrect
// ... rest of code
end;
应用程序 2,idtcpserver 执行事件(第二次尝试):
procedure TForm1.IdTCPServerExecute(AContext: TIdContext);
var c:ansistring;
begin
c:= AContext.Connection.IOHandler.ReadLn(Char(0));
// Here I expect c[0] = 48, c[1]=227, c[2]=0.
// But I get: c[0] = 48, c[1]=63, c[2]=0. c[1] is incorrect
// ... rest of code
end;
最奇怪的是,这些应用程序是几年前用 Delphi 5 开发的,它们运行良好(使用 readln(char(0))。当我将这两个应用程序翻译到 Delphi 2010 时,它们停止工作。我想这是由于 unicode字符串,但我还没有找到解决方案。
【问题讨论】:
标签: delphi-2010 indy10