我这个文件夹是空的下面这样吗 发送 procedure TForm1.Button4Click(Sender: TObject); var scmdatabyte: array of byte; scmData: String; i:Integer; begin scmData:='1A'; SetLength(scmdatabyte, Trunc(Length(scmdata) / 2)); for i:= 0 to Trunc(Length(scmdata)/2)-1 do begin scmdatabyte[i] := StrToInt('$' + scmdata[2 * i] + scmdata[2 * i + 1]); end; IdTCPClient1.WriteBuffer(scmdatabyte,10);end;接收 procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread); var i,ilen,count: integer; buf: array of byte; begin
try count:=AThread.Connection.ReadFromStack; if count<>0 then begin setlength(buf, count); AThread.Connection.ReadBuffer(buf[0], count); end; Memo1.Lines.Add(inttostr(count)); finally end;
end;
这和进制没关系,只和你发送机制有问题,writeln是以回车换行为结尾发送数据。补下网络编程基础。
给你个例子var wdata : array[0..100] of byte; wLen : Integer; begin s := Edit1.text; // 你要发的内容 wLen := Length(s); move(wdata[0], s[1], wLen); // 发送 idTcpClient.writeBuffer(wdata, wlen);
// 读信息用 idTcpClient.ReadBuffer就可以了 end;
谢谢我想问下为什么用固定的数组可以发送,为什么用动态数组却不能发送呢? 接收那边每次值都会变化 var sbuf: array of byte; i,j: Integer; begin SetLength(sbuf,28); for j := 0 to 28-1 do begin sbuf[j]:=StrToInt('$AA'); end; AThread.Connection.WriteBuffer(sbuf,length(sbuf));用sbuf2: array[1..28] of byte; 是正常的
动态数组的发送,可按下面方式发送 var sbuf: array of byte; i,j: Integer; begin SetLength(sbuf,28); for j := 0 to 28-1 do begin sbuf[j]:=StrToInt('$AA'); end; for j := 0 to 28-1 do begin AThread.Connection.WriteBuffer(sbuf[j],1); end;
我直接用IdTCPClient1.WriteLn(Edit1.Text); 来发对方说收不到,要用16进制发
发送
procedure TForm1.Button4Click(Sender: TObject);
var
scmdatabyte: array of byte;
scmData: String;
i:Integer;
begin
scmData:='1A';
SetLength(scmdatabyte, Trunc(Length(scmdata) / 2));
for i:= 0 to Trunc(Length(scmdata)/2)-1 do
begin
scmdatabyte[i] := StrToInt('$' + scmdata[2 * i] + scmdata[2 * i + 1]);
end; IdTCPClient1.WriteBuffer(scmdatabyte,10);end;接收
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
i,ilen,count: integer;
buf: array of byte;
begin
try
count:=AThread.Connection.ReadFromStack;
if count<>0 then
begin
setlength(buf, count);
AThread.Connection.ReadBuffer(buf[0], count);
end;
Memo1.Lines.Add(inttostr(count));
finally end;
end;
wdata : array[0..100] of byte;
wLen : Integer;
begin
s := Edit1.text; // 你要发的内容 wLen := Length(s); move(wdata[0], s[1], wLen); // 发送
idTcpClient.writeBuffer(wdata, wlen);
// 读信息用 idTcpClient.ReadBuffer就可以了
end;
var
sbuf: array of byte;
i,j: Integer;
begin
SetLength(sbuf,28);
for j := 0 to 28-1 do
begin
sbuf[j]:=StrToInt('$AA');
end;
AThread.Connection.WriteBuffer(sbuf,length(sbuf));用sbuf2: array[1..28] of byte; 是正常的
var
sbuf: array of byte;
i,j: Integer;
begin
SetLength(sbuf,28);
for j := 0 to 28-1 do
begin
sbuf[j]:=StrToInt('$AA');
end;
for j := 0 to 28-1 do
begin
AThread.Connection.WriteBuffer(sbuf[j],1);
end;