Z
zml2006
Unregistered / Unconfirmed
GUEST, unregistred user!
我用TTCPServer写了个简单的例子,但是在关闭程序时有时会出现异常,有时不会。我没有在窗体上放TTCPServer控件,而是在Create事件中创建并初始化,在Destroy事件中释放。代码如下:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Sockets;type TForm1 = class(TForm) procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } FTCPServer: TTCPServer; procedure Accept(Sender: TObject; ClientSocket: TCustomIpClient); public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormDestroy(Sender: TObject);begin FreeAndNil(FTCPServer);end;procedure TForm1.FormCreate(Sender: TObject);begin FTCPServer := TTCPServer.Create(Self); FTCPServer.LocalHost := 'localhost'; FTCPServer.LocalPort := '2121'; FTCPServer.OnAccept := Accept; FTCPServer.Active := True;end;procedure TForm1.Accept(Sender: TObject; ClientSocket: TCustomIpClient);begin begin while true do begin if ClientSocket.WaitForData(0) then begin if CompareText(ClientSocket.Receiveln(), 'exit') = 0 then begin ClientSocket.Sendln('byebye you'); Exit; end; end; end; endend;end.帮忙看看是什么问题哦。