说明:客户端不知道服务器端发来的数据有多长,错误出在ThreadRecv.HandleReceive过程。
我的客户端接收线程,代码如下:
unit Unit2;interfaceuses
Classes,SysUtils;type
TThreadRecv=class(TThread)
private
Buf : Pchar;
procedure HandleRecv; protected
procedure Execute;override;
public
end;implementationuses Unit1;{ TThreadRecv }procedure TThreadRecv.Execute;
var
iLen : integer;
begin
repeat
try
iLen:=Form1.TcpClient.ReceiveBuf(Self.Buf,1024);
if iLen>0 then Synchronize(Self.HandleRecv);
except
end;
until Self.Terminated;
end;procedure TThreadRecv.HandleRecv;
begin
Form1.Memo.Lines.Add(string(Self.Buf));
end;end.
我的客户端接收线程,代码如下:
unit Unit2;interfaceuses
Classes,SysUtils;type
TThreadRecv=class(TThread)
private
Buf : Pchar;
procedure HandleRecv; protected
procedure Execute;override;
public
end;implementationuses Unit1;{ TThreadRecv }procedure TThreadRecv.Execute;
var
iLen : integer;
begin
repeat
try
iLen:=Form1.TcpClient.ReceiveBuf(Self.Buf,1024);
if iLen>0 then Synchronize(Self.HandleRecv);
except
end;
until Self.Terminated;
end;procedure TThreadRecv.HandleRecv;
begin
Form1.Memo.Lines.Add(string(Self.Buf));
end;end.
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00404327 in module 'Project1.exe'. Read of address 7A7A7A7A'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
unit Unit2;interfaceuses
Classes,SysUtils;type
TThreadRecv=class(TThread)
private
Buf : Pchar;
procedure HandleRecv; protected
procedure Execute;override;
public
constructor Create(Suspended:Boolean);
end;implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TThreadRecv.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TThreadRecv }constructor TThreadRecv.Create(Suspended: Boolean);
begin
inherited Create(Suspended);
GetMem(Self.Buf, 1024);
end;procedure TThreadRecv.Execute;
var
iLen : integer;
begin
repeat
try
iLen:=Form1.TcpClient.ReceiveBuf(Self.Buf,1024);
if iLen>0 then Synchronize(Self.HandleRecv);
except
end;
until Self.Terminated;
end;procedure TThreadRecv.HandleRecv;
begin
Form1.Memo.Lines.Add(string(Self.Buf));
end;end.