注册 登录
编程论坛 Delphi论坛

使用 IdTCPClient1 和 IdTCPServer1 发送接收 16 进制数据的问题

mingzy 发布于 2014-06-05 13:52, 4664 次点击
我从网上找了些例子,测试结果都不太对,请教一下哪里有问题
发送数据如下
procedure Tmainfrm.Button1Click(Sender: TObject);
var
scmdatabyte: array of byte;
scmData: String;
i:Integer;
begin
 scmData:='1A1B1C1D1E1F10121314';
 SetLength(scmdatabyte, Trunc(Length(scmdata) / 2));
 for i:= 0 to Trunc(Length(scmdata)/2)-1 do
 begin
 scmdatabyte[i] := StrToInt('$' + scmdata[2 * i+1] + scmdata[2 * i + 2]);
 end;
 IdTCPClient1.WriteBuffer(scmdatabyte,10);
end;
接收
procedure Tmainfrm.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  i,ilen,count: integer;
      buf: array of byte;
      temp:string;
begin
try
    temp:='';
    count:=AThread.Connection.ReadFromStack;
    if count<>0 then
    begin
      setlength(buf, count);
      AThread.Connection.ReadBuffer(buf[0], count);
    end;
    for i:=0 to count-1 do
    temp:=temp+inttohex(buf[i],2);
    Memo1.Lines.Add(temp);
  finally
  end;
end;
接收到的数并不是我发出的数据
1 回复
#2
volte2014-06-05 15:19
你这样定义的包很有问题,没有一定的包格式,也没有包的校验;这样如果发送了脏数据包。你不是也处理。
好好从网上学习下数据包定义规范!再写程序吧
1