Socket的问题?还是线程的问题?总之是个怪问题! ( 积分: 100 )

  • 主题发起人 主题发起人 Miros
  • 开始时间 开始时间
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.
 
连回都没得回?不过自己搞定了!谁来接个分。。。。
 
我来听课,呵呵
 
这个论坛就是这个不好,不能撤消问题
 
什么原因仔细说说。。让我们也学习一下啊 谢了
 
虽然楼主自己解决了问题,那么也请把解题的思路或者方法共享一下,也可以给以后有类似问题的学弟学妹们指个路。
 
我也没解决,只是我没用他提供的Socket控件而是直接用api了。结果就好使了。
 
为什么无法给分?
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
566
import
I
I
回复
0
查看
571
import
I
后退
顶部