多线程产生的错误,那位帮帮忙啊! (50分)

  • 主题发起人 主题发起人 cb1997
  • 开始时间 开始时间
C

cb1997

Unregistered / Unconfirmed
GUEST, unregistred user!
部分程序如下
程序主要作用是多线程传输文件,客户端没问题,壳户端用多线程连接,服务器端接收数据时有点问题(服务器端可进行多个连接)
我自己找了找觉得好象就是带//???????????????????????? 的那两句好象有点冲突,我该如何修改啊
//服务器端
。。。。。
type
PClient = ^TClient;
TClient = record
DNS : String[20];
Connected,
LastAction : TDateTime;
Thread : Pointer;
end;

TDataState = (dstNone, dstReceiving);
TThreadData = class
private
FState: TDataState;
FFileSize: Integer;
FStream: TFileStream;
procedure SetState(const Value: TDataState);
procedure SetFileSize(const Value: Integer);
procedure SetStream(const Value: TFileStream);
public
constructor Create;
destructor Destroy;
override;
property State : TDataState read FState write SetState;
property FileSize : Integer read FFileSize write SetFileSize;
property Stream : TFileStream read FStream write SetStream;
end;
。。。。。。
procedure TfrmMain.IdTCPSConnect(AThread: TIdPeerThread);
var
NewClient: PClient;
begin
GetMem(NewClient,sizeOf(TClient));
NewClient.DNS := AThread.Connection.LocalName;
NewClient.Connected := Now;
NewClient.LastAction := NewClient.Connected;
NewClient.Thread := AThread;
[red] //???????????????????????? AThread.Data := TObject(NewClient);[/red]
Mehis.Lines.Add(TimeTOStr(time) + NewClient.DNS + '成功连入本机!');
try
CltThrdList.LockList.Add(NewClient);
finally
CltThrdList.UnlockList;
end;
end;

procedure TfrmMain.IdTCPSExecute(AThread: TIdPeerThread);
begin
[red]//???????????????????????? with AThread.Data as TThreadDatado
[/red]
begin
if State = DstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
。。。。
end;
end
else
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
。。。。。。。
end;
end;
end;
end;
 
怎么没有人帮忙呀!
我自己检查了一下,感觉是那两句有点问题,可是不知道怎么改!
小弟我也才接触这个不久,兄弟们拉小弟一把嘛
 
我在找你红色的句子呢
 
可能有些富翁看不到颜色,现在是加
//???????????????????????? 的那两句
 
怎么没有人帮忙呀,我把它简化一下,让大家更清楚点
。。。。。
type
PClient = ^TClient;
TClient = record
DNS : String[20];
Connected,
LastAction : TDateTime;
Thread : Pointer;
end;

TDataState = (dstNone, dstReceiving);
TThreadData = class
private
FState: TDataState;
FFileSize: Integer;
FStream: TFileStream;
procedure SetState(const Value: TDataState);
procedure SetFileSize(const Value: Integer);
procedure SetStream(const Value: TFileStream);
public
constructor Create;
destructor Destroy;
override;
property State : TDataState read FState write SetState;
property FileSize : Integer read FFileSize write SetFileSize;
property Stream : TFileStream read FStream write SetStream;
end;
。。。。。。
procedure TfrmMain.IdTCPSConnect(AThread: TIdPeerThread);
var
NewClient: PClient;
begin
GetMem(NewClient,sizeOf(TClient));
NewClient.DNS := AThread.Connection.LocalName;
NewClient.Connected := Now;
NewClient.LastAction := NewClient.Connected;
NewClient.Thread := AThread;
//????????????????????????
AThread.Data := TObject(NewClient);
//????????????????????????
end;
procedure TfrmMain.IdTCPSExecute(AThread: TIdPeerThread);
begin
if Not AThread.Terminated And AThread.Connection.Connected then
//?????????????????????????
with AThread.Data as TThreadDatado
//?????????????????????
begin
......................
end;
end;
我在IdTCPSConnect事件中将 AThread.Data := TObject(NewClient);
可是IdTCPSExecute,我有要将 AThread.Data as TThreadData,这两句是不是有冲突啊!
 
老兄,你定义的线程里头怎么连execute都没有,觉得有点奇怪。
 
TDataState = (dstNone, dstReceiving);
TThreadData = class
private
FState: TDataState;
FFileSize: Integer;
FStream: TFileStream;
procedure SetState(const Value: TDataState);
procedure SetFileSize(const Value: Integer);
procedure SetStream(const Value: TFileStream);
public
constructor Create;
destructor Destroy;
override;
property State : TDataState read FState write SetState;
property FileSize : Integer read FFileSize write SetFileSize;
property Stream : TFileStream read FStream write SetStream;
end;
是我定义 的一个类,主要是处理文件流的,不是定义的线程
 
对不起,没看清楚,帮你顶一下。:)
 
一条指令读写一个整型长度(32位系统下是4字节)之内的数据不需要考虑多线程冲突。所以,问题应该不是你标???????的地方,而可能是出现在其后的Mehis.Lines.Add...(这个最可疑)和你标。。。省略的地方
 
TO Another_eYes:
兄弟,我读写的是个文件流啊,我把那两个事件的全部代码都贴出来帮我看看吧!
procedure TfrmMain.IdTCPSConnect(AThread: TIdPeerThread);
var
NewClient: PClient;
begin
GetMem(NewClient,sizeOf(TClient));
NewClient.DNS := AThread.Connection.LocalName;
NewClient.Connected := Now;
NewClient.LastAction := NewClient.Connected;
NewClient.Thread := AThread;
Athread.Data := TObject(NewClient);
Mehis.Lines.Add(TimeTOStr(time) + NewClient.DNS + '成功连入本机!');
try
CltThrdList.LockList.Add(NewClient);
finally
CltThrdList.UnlockList;
end;

end;

procedure TfrmMain.IdTCPSExecute(AThread: TIdPeerThread);
var
aFileSize:integer;
aFileName:string;
buf:array[0..1023] of byte;
ReadCount:integer;
begin
if Not AThread.Terminated And AThread.Connection.Connected then
with AThread.Data as TThreadDatado
begin
if State = DstNone then
begin
aFileName := AThread.Connection.ReadLn(#13#10,100);
if application.MessageBox('有数据传入,是否接收?','消息框',4 + 32) = id_no then
begin
AThread.Connection.WriteLn('拒绝接收!');
Mehis.Lines.Add('拒绝接收数据!');
exit;
end else
if aFileName = '' then
exit;
SaveDialog.FileName := aFileName;
if SaveDialog.Execute then
begin
AThread.Connection.WriteLn();
AThread.Connection.ReadBuffer(aFileSize,4);
FileSize := aFileSize;
Stream := TFileStream.Create(SaveDialog.FileName,fmcreate);
State := dstReceiving;
end else
AThread.Connection.WriteLn('对方取消接收!');
end
else
begin
repeat
if FileSize - Stream.Size > SizeOf(buf) then
ReadCount := SizeOf(buf)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(buf,ReadCount);
Stream.WriteBuffer(buf,ReadCount);
Application.ProcessMessages;
until Stream.Size >= FileSize;
Stream.Free;
Stream := nil;
AThread.Connection.WriteLn('接收完毕!');
Mehis.Lines.Add('数据接收完毕!');
State := dstNone;
end;
end;
end;
 
后退
顶部