unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,winsock, StdCtrls,unit2;type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public sock2:TSocket; { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); var datas :WSAData; socket1:TSOCKET; addr:sockaddr_in; hostent1:hostent; addrclient:sockaddr_in; lent:integer; thread_s:S_Client; begin if WSAStartup (1,datas)<>0 then ShowMessage('Error'); socket1:=socket(AF_INET, SOCK_STREAM ,0); if socket1=INVALID_SOCKET then ShowMessage('scoket error') ; hostent1:=(gethostbyname('smtp.163.net'))^; addr.sin_family :=AF_INET; addr.sin_port:=htons(1001); addr.sin_addr.S_addr:=INADDR_ANY; if bind(socket1,addr,sizeof(addr))<>0 then ShowMessage('bind error'); if listen(socket1,1)<>0 then ShowMessage('listen error'); while True do begin sock2:=accept(socket1, @addrclient,@lent); thread_s.Create(sock2,memo1); Application.ProcessMessages ; end end;end.//---------------------------------------------------------------- unit Unit2;interfaceuses Classes,winsock,StdCtrls;type S_Client = class(TThread) // private { Private declarations } sock_s :TSOCKET; memo:TMemo; procedure Receive(); public constructor Create(sock:TSOCKET;memo1:TMemo); protected procedure Execute; override; end;implementation{ Important: Methods and properties of objects in VCL or CLX can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure S_Client.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ S_Client }procedure S_Client.Execute; begin { Place thread code here } end; constructor S_Client.Create(sock:TSOCKET;memo1:TMemo) ; begin memo:=memo1 ; sock_s:=sock; inherited create(false);end; procedure S_Client.Receive(); //TYPE // PChar =^Char; var buf:^Char; begin getmem(buf,1024); recv(sock_s,buf,1024,0); memo.Lines.Add(buf^); end; end.
inherited create(false);产生的
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls,unit2;type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
sock2:TSocket;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
datas :WSAData;
socket1:TSOCKET;
addr:sockaddr_in;
hostent1:hostent; addrclient:sockaddr_in;
lent:integer;
thread_s:S_Client;
begin if WSAStartup (1,datas)<>0 then
ShowMessage('Error');
socket1:=socket(AF_INET, SOCK_STREAM ,0);
if socket1=INVALID_SOCKET then
ShowMessage('scoket error') ;
hostent1:=(gethostbyname('smtp.163.net'))^;
addr.sin_family :=AF_INET;
addr.sin_port:=htons(1001);
addr.sin_addr.S_addr:=INADDR_ANY;
if bind(socket1,addr,sizeof(addr))<>0 then
ShowMessage('bind error');
if listen(socket1,1)<>0 then
ShowMessage('listen error');
while True do
begin
sock2:=accept(socket1, @addrclient,@lent);
thread_s.Create(sock2,memo1);
Application.ProcessMessages ;
end
end;end.//----------------------------------------------------------------
unit Unit2;interfaceuses
Classes,winsock,StdCtrls;type
S_Client = class(TThread)
//
private
{ Private declarations }
sock_s :TSOCKET;
memo:TMemo;
procedure Receive();
public
constructor Create(sock:TSOCKET;memo1:TMemo);
protected
procedure Execute; override;
end;implementation{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure S_Client.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ S_Client }procedure S_Client.Execute;
begin
{ Place thread code here }
end;
constructor S_Client.Create(sock:TSOCKET;memo1:TMemo) ;
begin memo:=memo1 ;
sock_s:=sock;
inherited create(false);end;
procedure S_Client.Receive();
//TYPE
// PChar =^Char;
var buf:^Char;
begin
getmem(buf,1024);
recv(sock_s,buf,1024,0);
memo.Lines.Add(buf^);
end;
end.
inherited create(false);就 AV了
memo:=memo1 ;
sock_s:=sock;
可以吗
memo:=memo1 ;
sock_s:=sock;
可以吗
还是AV
你只定义了memo:TMemo;但是它的内存地址呢???在线程create的时候,一起把memo等一起create;再试试吧
如果可以,请发到我的信息[email protected]