Home > Back-end >  Socket error # 10004 closed TIdUDPServer class object, attach all code, can run the code, please adv
Socket error # 10004 closed TIdUDPServer class object, attach all code, can run the code, please adv

Time:09-23

Create a TUDPClass communication class, such as when released, the socket # 10004, don't know why, and guide the warrior,
Attach all the code below, in the delphi2007 run:


The program Project1.

USES the
Forms,
The Main in 'Main. Pas' {Form1},
MyUdp in 'MyUdp. Pas';

{$R *. Res}

The begin
Application. The Initialize;
Application. MainFormOnTaskbar:=True;
CreateCommunicate;//create communication class object
Application CreateForm (TForm1 Form1);
Application. The Run;
End.


==========================================================

The unit Main;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MyUdp;

Type
TCommunicationClass=class (TObject)
Private
_MyUdp: TUDPClass;
Public
The constructor Create; The phrase ";
Constructor created (port: Integer); The phrase ";
The destructor Destroy; Override.
end;

TForm1=class (TForm)
Procedure FormDestroy (Sender: TObject);//in the main form is released release MyUdp class object.
Private
{Private declarations}
Public
{Public declarations}

end;

Procedure CreateCommunicate;//create communication class object
Procedure DestoryCommunicate;//release communication class object

Var
Form1: TForm1;
FComManager: TCommunicationClass;

Implementation

{$R *. DFM}
Procedure CreateCommunicate;
The begin
FComManager:=TCommunicationClass. Create (124);
end;

Procedure DestoryCommunicate;
The begin
If Assigned (FComManager) then
FreeAndNil (FComManager);
end;

Procedure TForm1. FormDestroy (Sender: TObject);
The begin
DestoryCommunicate;
end;

{TCommunicationClass}
The constructor TCommunicationClass. Create;
The begin
If not Assigned (_MyUdp) then
The begin
_MyUdp:=TUDPClass. Create (9523);
_MyUdp. Active:=True;
end;
end;

Constructor TCommunicationClass. Create (port: Integer);
The begin
If not Assigned (_MyUdp) then
The begin
_MyUdp:=TUDPClass. Create (port);
end;
end;

Destructor TCommunicationClass. Destroy;
The begin
If Assigned (_MyUdp) then
The begin
Try
_MyUdp. Active:=False;
FreeAndNil (_MyUdp);
Except on E: the Exception do
FreeAndNil (_MyUdp);
end;
end;
Inherited;
end;

End.

==========================================================

The unit MyUdp;

Interface

USES the
IdUDPServer, Classes, IdSocketHandle, Messages, Windows, SysUtils, IdStack,
IdGlobal IdException, Dialogs, forms;

Type
TUDPClass=class
Private
FUDPServer: TIdUDPServer;

FPort: Integer;
The function GetActive: Boolean;
Procedure SetActive (const Value: Boolean);
Public
Constructor created (Port: Integer);
The destructor Destroy; Override.
The property Active: Boolean read GetActive write SetActive;
end;

Implementation

{TUDPControl}

Constructor TUDPClass. Create (Port: Integer);
The begin
FUDPServer:=TIdUDPServer. Create (nil);
FUDPServer. Bindings. The Clear;
FUDPServer. DefaultPort:=Port;
FPort:=Port;

If not fUDPServer. Active then
The begin
Try
fUDPServer. Active:=True;//as long as to enable the bank code, when the code runs TCommunicationClass. Destroy the
//_MyUdp. Active:=False this line, will be an error socket # 10004. Why???

Except,
On e: the Exception do
The begin
If e is EIdCouldNotBindSocket then
The begin
end;
end;
end;
end;

FPort:=Port;
end;

Destructor TUDPClass. Destroy;
The begin
FUDPServer. Active:=False;
FUDPServer. Free;
Inherited;
end;

The function TUDPClass. GetActive: Boolean;
The begin
Result:=fUDPServer. Active;
end;

Procedure TUDPClass. SetActive (const Value: Boolean);
The begin
FUDPServer. Active:=Value;
end;

End.


Please feel free to comment, thank you
  • Related