说明:客户端不知道服务器端发来的数据有多长,错误出在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.

解决方案 »

  1.   

    The Error:---------------------------
    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   
    ---------------------------
      

  2.   

    这是地址错误,是不是你的buf没有申请内存。这里是一个指针,要申请内存才能读写。
      

  3.   

    我现在又申请了内存,可是还是错误。New code as following:
    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.