M
Miros
Unregistered / Unconfirmed
GUEST, unregistred user!
(*
代码不多,不过问题跟奇怪。
如果TA是继承自TThread类那么ClientSocket就会无休止的连接服务器。
如果TA不是继承自TThread类那么一切都很顺利!
希望高手说说是什么个问题。
如果可以让TA即继承TThread类有让ClientSocket正常工作,那当然更好。
*)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
const
CLASSISTTHREAD = FALSE;//把这改成TRUE ClientSocket就坏了。
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
m_ServerSocket : TServerSocket;
procedure ServerSocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocketOnDisConnect(Sender: TObject; Socket: TCustomWinSocket);
{ Private declarations }
public
{ Public declarations }
end;
TA = class{$IF CLASSISTTHREAD}(TThread){$IFEND}
private
m_Socket: TClientSocket;
m_dwNextReConnectTime : Cardinal;
function ConfimConnected():Boolean;
procedure DisConnect();
procedure SocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure SocketConnected();
protected
procedure Execute;{$IF CLASSISTTHREAD}override;{$IFEND}
public
constructor Create(sSrvAddr: string; wSrvPort: Word);{$IF CLASSISTTHREAD}reintroduce;{$IFEND}
destructor Destroy;override;
end;
var
Form1: TForm1;
A : TA;
{$IF NOT CLASSISTTHREAD}
g_boExiting : Boolean = False;
{$IFEND}
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
{ TA }
function TA.ConfimConnected: Boolean;
begin
Result := m_Socket.Socket.Connected;
if not Result and (GetTickCount() >= m_dwNextReConnectTime) then
begin
m_dwNextReConnectTime := GetTickCount() + 3 * 1000;
m_Socket.Active := True;
end;
end;
constructor TA.Create(sSrvAddr: string; wSrvPort: Word);
begin
Inherited Create{$IF CLASSISTTHREAD}(true){$IFEND};
m_Socket := TClientSocket.Create(nil);
m_Socket.Address := sSrvAddr;
m_Socket.Port := wSrvPort;
m_Socket.OnConnect := SocketOnConnect;
m_dwNextReConnectTime := GetTickCount();
end;
destructor TA.Destroy;
begin
DisConnect();
m_Socket.Free;
inherited;
end;
procedure TA.DisConnect;
begin
m_Socket.Active := False;
end;
procedure TA.Execute;
begin
while not {$IF CLASSISTTHREAD}Terminated{$ELSE}g_boExiting{$IFEND} do
begin
ConfimConnected();
{$IF NOT CLASSISTTHREAD}
Application.ProcessMessages();
{$IFEND}
end;
end;
procedure TA.SocketConnected;
begin
ShowMessage('Conneced!');
end;
procedure TA.SocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
{$IF CLASSISTTHREAD}
Synchronize(SocketConnected);
{$ELSE}
SocketConnected();
{$IFEND}
end;
procedure TForm1.FormActivate(Sender: TObject);
const
{$J+}
boFirstActivate: Boolean = TRUE;
{$J-}
begin
{$IF NOT CLASSISTTHREAD}
if not boFirstActivate then
Exit;
boFirstActivate := False;
A.Execute;
{$IFEND}
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{$IF CLASSISTTHREAD}
A.Terminate;
{$ELSE}
g_boExiting := True;
{$IFEND}
A.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m_ServerSocket := TServerSocket.Create(Self);
m_ServerSocket.Port := 3327;
m_ServerSocket.OnClientConnect := ServerSocketOnConnect;
m_ServerSocket.OnClientDisconnect := ServerSocketOnDisConnect;
m_ServerSocket.Active := True;
A := TA.Create('127.0.0.1', 3327);
{$IF CLASSISTTHREAD}
A.Resume;
{$IFEND}
end;
procedure TForm1.ServerSocketOnConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Format('Connected(%s:%d)...', [Socket.RemoteAddress, Socket.RemotePort]))
end;
procedure TForm1.ServerSocketOnDisConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Format('DisConnected(%s:%d)...', [Socket.RemoteAddress, Socket.RemotePort]))
end;
end.
代码不多,不过问题跟奇怪。
如果TA是继承自TThread类那么ClientSocket就会无休止的连接服务器。
如果TA不是继承自TThread类那么一切都很顺利!
希望高手说说是什么个问题。
如果可以让TA即继承TThread类有让ClientSocket正常工作,那当然更好。
*)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
const
CLASSISTTHREAD = FALSE;//把这改成TRUE ClientSocket就坏了。
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
m_ServerSocket : TServerSocket;
procedure ServerSocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocketOnDisConnect(Sender: TObject; Socket: TCustomWinSocket);
{ Private declarations }
public
{ Public declarations }
end;
TA = class{$IF CLASSISTTHREAD}(TThread){$IFEND}
private
m_Socket: TClientSocket;
m_dwNextReConnectTime : Cardinal;
function ConfimConnected():Boolean;
procedure DisConnect();
procedure SocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure SocketConnected();
protected
procedure Execute;{$IF CLASSISTTHREAD}override;{$IFEND}
public
constructor Create(sSrvAddr: string; wSrvPort: Word);{$IF CLASSISTTHREAD}reintroduce;{$IFEND}
destructor Destroy;override;
end;
var
Form1: TForm1;
A : TA;
{$IF NOT CLASSISTTHREAD}
g_boExiting : Boolean = False;
{$IFEND}
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
{ TA }
function TA.ConfimConnected: Boolean;
begin
Result := m_Socket.Socket.Connected;
if not Result and (GetTickCount() >= m_dwNextReConnectTime) then
begin
m_dwNextReConnectTime := GetTickCount() + 3 * 1000;
m_Socket.Active := True;
end;
end;
constructor TA.Create(sSrvAddr: string; wSrvPort: Word);
begin
Inherited Create{$IF CLASSISTTHREAD}(true){$IFEND};
m_Socket := TClientSocket.Create(nil);
m_Socket.Address := sSrvAddr;
m_Socket.Port := wSrvPort;
m_Socket.OnConnect := SocketOnConnect;
m_dwNextReConnectTime := GetTickCount();
end;
destructor TA.Destroy;
begin
DisConnect();
m_Socket.Free;
inherited;
end;
procedure TA.DisConnect;
begin
m_Socket.Active := False;
end;
procedure TA.Execute;
begin
while not {$IF CLASSISTTHREAD}Terminated{$ELSE}g_boExiting{$IFEND} do
begin
ConfimConnected();
{$IF NOT CLASSISTTHREAD}
Application.ProcessMessages();
{$IFEND}
end;
end;
procedure TA.SocketConnected;
begin
ShowMessage('Conneced!');
end;
procedure TA.SocketOnConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
{$IF CLASSISTTHREAD}
Synchronize(SocketConnected);
{$ELSE}
SocketConnected();
{$IFEND}
end;
procedure TForm1.FormActivate(Sender: TObject);
const
{$J+}
boFirstActivate: Boolean = TRUE;
{$J-}
begin
{$IF NOT CLASSISTTHREAD}
if not boFirstActivate then
Exit;
boFirstActivate := False;
A.Execute;
{$IFEND}
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{$IF CLASSISTTHREAD}
A.Terminate;
{$ELSE}
g_boExiting := True;
{$IFEND}
A.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m_ServerSocket := TServerSocket.Create(Self);
m_ServerSocket.Port := 3327;
m_ServerSocket.OnClientConnect := ServerSocketOnConnect;
m_ServerSocket.OnClientDisconnect := ServerSocketOnDisConnect;
m_ServerSocket.Active := True;
A := TA.Create('127.0.0.1', 3327);
{$IF CLASSISTTHREAD}
A.Resume;
{$IFEND}
end;
procedure TForm1.ServerSocketOnConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Format('Connected(%s:%d)...', [Socket.RemoteAddress, Socket.RemotePort]))
end;
procedure TForm1.ServerSocketOnDisConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Format('DisConnected(%s:%d)...', [Socket.RemoteAddress, Socket.RemotePort]))
end;
end.