我用的是在论坛里常搜索到的代码服务端:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
//从这里开始似乎一连接上后就不停的执行
iFileHandle:=FileOpen('d:\ftp\qq2004sp1.txt',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
AThread.Connection.WriteInteger(iFileLen);
while true do
begin
cnt:=FileRead(iFileHandle,buf,4096);
AThread.Connection.WriteBuffer(buf,cnt);
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
end;客户端:
procedure TForm1.Button1Click(Sender: TObject);
var
rbyte:array[0..4096] of byte;
sFile:TFileStream;
iFileSize:integer;
begin
try
IdTcpClient1.Connect(5000);
except
exit;
end; iFileSize:=IdTCPClient1.ReadInteger; //这里读到的iFileSize始终为-1 sFile:=TFileStream.Create('e:\bb.tmp',fmCreate);
While iFileSize>4096 do
begin
IdTCPClient1.ReadBuffer(rbyte,4096);
sFile.Write(rByte,4096);
inc(iFileSize,-4096);
end;
IdTCPClient1.ReadBuffer(rbyte,iFileSize);
sFile.Write(rByte,iFileSize);
sFile.Free;
ShowMessage('file get ok!');
end;这个代码是不是有问题?或者谁有更完整的代码给我参考?
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
//从这里开始似乎一连接上后就不停的执行
iFileHandle:=FileOpen('d:\ftp\qq2004sp1.txt',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
AThread.Connection.WriteInteger(iFileLen);
while true do
begin
cnt:=FileRead(iFileHandle,buf,4096);
AThread.Connection.WriteBuffer(buf,cnt);
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
end;客户端:
procedure TForm1.Button1Click(Sender: TObject);
var
rbyte:array[0..4096] of byte;
sFile:TFileStream;
iFileSize:integer;
begin
try
IdTcpClient1.Connect(5000);
except
exit;
end; iFileSize:=IdTCPClient1.ReadInteger; //这里读到的iFileSize始终为-1 sFile:=TFileStream.Create('e:\bb.tmp',fmCreate);
While iFileSize>4096 do
begin
IdTCPClient1.ReadBuffer(rbyte,4096);
sFile.Write(rByte,4096);
inc(iFileSize,-4096);
end;
IdTCPClient1.ReadBuffer(rbyte,iFileSize);
sFile.Write(rByte,iFileSize);
sFile.Free;
ShowMessage('file get ok!');
end;这个代码是不是有问题?或者谁有更完整的代码给我参考?
什麼不用 Indy.WriteStream() 呢??? 這樣更直接呀.
[email protected]