在INDY中的Idtcpclient的readln方法中,是不是没有读到就一直等待???_没分了,下次给(3分)

  • 主题发起人 主题发起人 lk007
  • 开始时间 开始时间
L

lk007

Unregistered / Unconfirmed
GUEST, unregistred user!
在INDY中的Idtcpclient的readln方法中,是不是没有读到就一直等待???
还有象这句IdTCPClient1.ReadLn(#13#10,1000);中的1000 文档里说是超时参数. 可这里指什么超时? 超了时又会怎么样?? 好象看不出来.
大侠能不能占用你一点时间,帮我解决一下,搞了很久了,就是没搞定!!!!!!!!

procedure TfmClient.Button1Click(Sender: TObject);
var
Buf : array[0..1023] of Byte;
ReadCount : Integer;
aStream : TFileStream;
aSize : Integer;
aFileName : String;
begin
if OpenDialog1.Execute then
begin
IdTCPClient1.Connect;
aStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyWrite);
try
//发送文件名
aFileName := ExtractFileName(OpenDialog1.FileName);
IdTCPClient1.WriteLn(aFileName);
//等待接受确认
edit3.Text:= IdTCPClient1.ReadLn(#13#10,1000); ////如果服务器没有执行那条writeln则程序一直等在此句里??????????????????????????
//写文件长度和文件流
aSize := aStream.Size;
ProgressBar1.Max := aSize;
IdTCPClient1.WriteBuffer(aSize, 4);
//IdTCPClient1.WriteStream(aStream);
while aStream.Position < aStream.Size do
begin
if aStream.Size - aStream.Position >= SizeOf(Buf) then
ReadCount := sizeOf(Buf)
else
ReadCount := aStream.Size - aStream.Position;
aStream.ReadBuffer(Buf, ReadCount);
IdTCPClient1.WriteBuffer(Buf, ReadCount);
ProgressBar1.Position := aStream.Position;
Application.ProcessMessages;
end;
Caption := IdTCPClient1.ReadLn;
IdTCPClient1.Disconnect;
finally
edit3.Text:='finally';
aStream.Free;
end;
end;
end;
=======================
procedure TfmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
aFileSize : Integer;
aFileName : String;
Buff : array[0..1023] of Byte;
ReadCount : Integer;
begin
with AThread.Data as TThreadData do
begin
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件名
aFileName := AThread.Connection.ReadLn(#13#10, 100);
if aFileName = '' then
Exit;
SaveDialog1.FileName := aFileName;
// if SaveDialog1.Execute then
//begin
//返回确认文件传输标志-----------如果把下面一句注释了,则客户端会一直停在如上所注的那行!!!!!!!!!!!!!!!!!!!!!1不知为何????
AThread.Connection.WriteLn('test');
//开始读取文件长度,创建文件
AThread.Connection.ReadBuffer(aFileSize, 4);
FileSize := aFileSize;
ProgressBar1.Max := FileSize;
Stream := TFileStream.Create('i:/' +aFileName, fmCreate);
State := dstReceiving;
//end
//else
// AThread.Connection.Disconnect
end;
end else begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if FileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(Buff, ReadCount);
Stream.WriteBuffer(Buff, ReadCount);
ProgressBar1.Position := Stream.Size;
Caption := IntToStr(Stream.Size) + '/' + IntToStr(FileSize);
Application.ProcessMessages;
until Stream.Size >= FileSize;
AThread.Connection.WriteLn('OK');
Stream.Free;
Stream := nil;
State := dstNone;
end;
end;
end;
end;
 
这应该是INDY组件的一个BUG把。你打开IdTCPConnection.pas。找到
FReadLnTimedOut := ReadFromStack(True, ATimeout, ATimeout = IdTimeoutDefault) =0
(不是很清楚了。反正是0就改为-1,是-1就改0)
 
设置TIdTCPConnection.TimeOut属性。
 
后退
顶部