有人用过POWERTCP telnet这个控件吗?(100分)

  • 主题发起人 主题发起人 123456
  • 开始时间 开始时间
1

123456

Unregistered / Unconfirmed
GUEST, unregistred user!
有人用过POWERTCP telnet这个控件吗??我正在用POWERTCP的telnet控件加thread做多线程的telnet同时连接多个不同的服务器收数据,单独执行一个连接不用线程是成功的, 但是这段代码放在在BMDthread的excute过程里就有问题,blocking登陆后转为nonblocking模式收数据,怎么也收不到,thread好像停止了(执行完??)。怎么用thread的waitfor??
BMDThread这个控件:
http://www.mitov.com/Dwnd/dwnd.html
先问路,代码稍后贴出,望高手指点,谢谢!!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, BMDThread, DartTelnet_TLB, OleCtrls, DartVt_TLB;

type
TForm1 = class(TForm)
Button1: TButton;
Vt1: TVt;
Telnet1: TTelnet;
Telnet2: TTelnet;
Telnet3: TTelnet;
BMDThread1: TBMDThread;
BMDThread2: TBMDThread;
BMDThread3: TBMDThread;
BMDThreadGroup1: TBMDThreadGroup;
Edit1: TEdit;
procedure BMDThread1Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure BMDThread2Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure BMDThread3Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Telnet1Receive(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BMDThread1Execute(Sender: TObject;
Thread: TBMDExecuteThread; var Data: Pointer);
var
Data, Token: OleVariant;
msg: string;
begin
if gUser = '' then exit;

try
begin
// Set Timeout 60秒连接 . Scripting operations will occur in
// blocking mode(阻塞模式). Switch back to async after done with script
Telnet1.Timeout := 60000;
// Connect to the Server
Telnet1.AutoOption := True;
Telnet1.Connect('10.0.0.141', 23, '', 0);
end
except
on E:Exception do
begin
Application.MessageBox(PChar(E.Message), 'Connect Failed', MB_ICONEXCLAMATION);
exit;
end;
end;

try
begin
// only 等待20 秒登录界面
Telnet1.Timeout := 10000;

// Search for "ogin:" 搜索login
Data := '';
Token := 'ogin:';
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send User name
Data := 'root'#13#10;
Telnet1.Send(Data,0); //发用户名

// Search for "assword:" 搜索密码输入
Data := '';
Token := 'assword:';
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send Password
Data := '1234'#13#10;
Telnet1.Send(Data,0); //发送密码

Data := '';
Token := '#'; //搜索命令提示符
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send CMD
Data := 'ls -a'#13#10;
Telnet1.Send(Data,0); //发送ls命令
end
except
on E:Exception do
begin
msg := 'Scripted Login failed. A prompt was not found.'#13#10;
msg := msg + 'Check the code in ''DoScriptLogin'' and verify that you are searching ';
msg := msg + 'for the correct prompts.';
Application.MessageBox(PChar(msg), 'Prompt Not Found', MB_ICONEXCLAMATION);
end;
end;
// Switch back to async 转回非阻塞模式
Telnet1.Timeout := 0;

// 这里发送一个命令让telnet1接收长时间不断输出的数据
Data := 'CMD xxxxx'#13#10;
Telnet1.Send(Data,0); //发送
[red]//运行到这里是否已经退出thread?? 怎样waitfor一直让thread接收服务器送出的数据??(我要接收4小时才收完)[/red]
end;

//telnet接收的过程,23端口有数据时就触发,显示在VT上(相当于接收数据显示在Memo上,这个关系不大)
procedure TForm1.Telnet1Receive(Sender: TObject);
var
Data : OleVariant;
ba:array of byte;

begin
// If blocking, receive will occur in the function that is blocking
// if Telnet1.Timeout > 0 then exit;

// telnet has data ready to receive

// receive the data via a byte array
SetLength(ba,Telnet1.ReceiveBufferCount);
Data := ba;
Telnet1.Receive( Data);
Vt1.Display(Data);
end;

[purple]我不用thread做时完全正常,但用thread后在发完CMD xxxxxx后就收不到服务器回送的长数据了,是否要把Telnet1Receive的过程写在thread的excute里??
还是telnet1要在thread1excute里定义?还是不要用async(非阻塞)模式?但telnetTimeoute的时间不可能设到4小时啊!请帮忙[/purple]
[?][?]
 
1.TCP的阻塞和非阻塞模式不能实时切换
2.你可以使用DELPHI7+INDY9中的TIDTELNET控件来满足你需要.
3.你应该不用新建线程来处理数据接收,TIDTELNET会自动建立一个线程来接收数据,你只需要在ondataavailable中处理收到的数据即可.
4.如果你需要同时处理多个连接的话,你可以建立多个TidTelnet,可以用数组保存,然后你可能需要用到IdAntiFreeze控件,他可以防止在阻塞模式下,界面被冻结的情况.
 
后退
顶部