急,socket(100分)

  • 主题发起人 主题发起人 lishubing_2000
  • 开始时间 开始时间
L

lishubing_2000

Unregistered / Unconfirmed
GUEST, unregistred user!
SubmitSockClient2:= TClientSocket.Create(nil);
SubmitSockClient2.ClientType:=ctBlocking;
SubmitSockClient2.OnError :=form.SubmitSockClientError;
SubmitSockClient2.Address:=trim(form. sgipConfig.UniComIP);
SubmitSockClient2.Port:=form.sgipConfig.UniComPort;
form.log('***************SUBMIT START ACTIVE**********************');
SubmitSockClient2.Active:=true;
form.log('******************SUBMIT START END*******************');


这段程序,我在测试时 可已与服务器连接,可为什么加到我的程序中时就在
SubmitSockClient2.Active:=true;
这一行 停住了呢, 急
 
lienttype=ctNonBlocking
 
不是这问题,我在其他地方试过的,绑定模式 是可以连接成功的
 
ctBlocking要放入线程。

两个单元:Unit1.pas:主程序;MyThread.pas:线程的主程序;

//Unit1.pas
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, MyThread;

type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
tmBegin : TTime;
CT: TMyThread;
CTActive: Boolean;
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation


{$R *.dfm}


procedure TMainForm.Button1Click(Sender: TObject);
var
iTimeOut: Integer;//从Open到现在经过的秒数
begin
CT := TMyThread.CreateIt;

tmBegin := Time;
CTActive := True;
CT.Resume;

while (CT <> nil) and (CTActive) and (not CT.CS.Active) do
begin
iTimeOut := StrToInt(FormatDateTime('ns' , Time - tmBegin));

if iTimeOut >= 5 then//超时5秒
begin
CT.Terminate;
MessageBox(Handle, PChar('因为超时,无法连接到目的计算机.'), '连接失败...', MB_ICONERROR);
Exit;
end;
end;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if CT <> nil then CT.Terminate;
end;

end.

//MyThread.pas
unit MyThread;

interface

uses
Classes, comctrls, ScktComp, Dialogs;

type
TMyThread = class(TThread)
private
FCS: TClientSocket;
procedure CSConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure CSError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
protected
procedure Execute; override;
public
constructor CreateIt;
destructor Destroy; override;
published
property CS: TClientSocket read FCS write FCS;
end;

var
MyThread1: TMyThread;

implementation

{ TMyThread }

constructor TMyThread.CreateIt;
begin
Inherited Create(False);
FCS := TClientSocket.Create(nil);
end;

procedure TMyThread.CSConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
ShowMessage('Connected!');
end;

procedure TMyThread.CSError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode := 0;
case ErrorEvent of
eeConnect: showMessage('Error in connecting');
else
end;
end;

destructor TMyThread.Destroy;
begin
if FCS.Active then FCS.Close;
FCS.Free;
inherited Destroy;
end;

procedure TMyThread.Execute;
begin
with FCS do
begin
OnConnect := MyThread1.CSConnect;
OnError := MyThread1.CSError;
ClientType := ctBlocking;
Address := '111.111.111.111';
Port := 9999;
Open;
end;
end;

end.
 
为什么单独运行你们的程序可以连接
可我 加到我的线程中就不行了呢

是不是有什么重点,我没有做
 
andy263 老师请回答
 
可以了
谢谢 andy263 老师
 

Similar threads

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