創建一個TUDPClass的通訊類,在釋放時此類時,報socket #10004,不知道原因,求大俠們指點迷津。
下面附上所有代碼,在delphi2007上運行:
program Project1;
uses
Forms,
Main in 'Main.pas' {Form1},
MyUdp in 'MyUdp.pas';
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
CreateCommunicate;//創建通訊類物件
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
==========================================================
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MyUdp;
type
TCommunicationClass = class(TObject)
private
_MyUdp: TUDPClass;
public
constructor Create; overload;
constructor Create(port: Integer); overload;
destructor Destroy; override;
end;
TForm1 = class(TForm)
procedure FormDestroy(Sender: TObject);//在主表單釋放時釋放MyUdp類物件.
private
{ Private declarations }
public
{ Public declarations }
end;
procedure CreateCommunicate; //創建通訊類物件
procedure DestoryCommunicate; //釋放通訊類物件
var
Form1: TForm1;
FComManager: TCommunicationClass;
implementation
{$R *.dfm}
procedure CreateCommunicate;
begin
FComManager:= TCommunicationClass.Create(124);
end;
procedure DestoryCommunicate;
begin
if Assigned(FComManager) then
FreeAndNil(FComManager);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DestoryCommunicate;
end;
{ TCommunicationClass }
constructor TCommunicationClass.Create;
begin
if not Assigned(_MyUdp) then
begin
_MyUdp:= TUDPClass.Create(9523);
_MyUdp.Active := True;
end;
end;
constructor TCommunicationClass.Create(port: Integer);
begin
if not Assigned(_MyUdp) then
begin
_MyUdp:= TUDPClass.Create(port);
end;
end;
destructor TCommunicationClass.Destroy;
begin
if Assigned(_MyUdp) then
begin
try
_MyUdp.Active := False;
FreeAndNil(_MyUdp);
except on E:Exception do
FreeAndNil(_MyUdp);
end;
end;
inherited;
end;
end.
==========================================================
unit MyUdp;
interface
uses
IdUDPServer, Classes, IdSocketHandle, Messages, Windows, SysUtils, IdStack,
IdGlobal,IdException, Dialogs, forms;
type
TUDPClass = class
private
fUDPServer : TIdUDPServer;
fPort: Integer;
function GetActive: Boolean;
procedure SetActive(const Value: Boolean);
public
constructor Create(Port: Integer);
destructor Destroy; override;
property Active: Boolean read GetActive write SetActive;
end;
implementation
{ TUDPControl }
constructor TUDPClass.Create(Port: Integer);
begin
fUDPServer := TIdUDPServer.Create(nil);
fUDPServer.Bindings.Clear;
fUDPServer.DefaultPort := Port;
fPort := Port;
if not fUDPServer.Active then
begin
try
fUDPServer.Active := True; //只要啟用本行代碼,當代碼運行TCommunicationClass.Destroy中的
// _MyUdp.Active := False這行,就會報錯socket #10004.為啥???
except
on e : Exception do
begin
if e is EIdCouldNotBindSocket then
begin
end;
end;
end;
end;
fPort := Port;
end;
destructor TUDPClass.Destroy;
begin
fUDPServer.Active := False;
fUDPServer.Free;
inherited;
end;
function TUDPClass.GetActive: Boolean;
begin
Result := fUDPServer.Active;
end;
procedure TUDPClass.SetActive(const Value: Boolean);
begin
fUDPServer.Active := Value;
end;
end.
請各位不吝賜教 ,謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/70459.html
標籤:VCL組件開發及應用
上一篇:Android做事實監控視頻后臺
