【Delphi】用indy写的Telnet客户端

Unit1.pas: 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ComCtrls, Unit2;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TRichEdit;
    ButtonSend: TButton;
    procedure WmAfterShow(var Msg: TMessage); message WM_User;
    procedure FormShow(Sender: TObject);
    procedure ButtonSendClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Memo1Change(Sender: TObject);
  private
    Telnet: TTelnet;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Position := poScreenCenter;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  OnShow := nil;
  PostMessage(Handle, WM_User, 0, 0);
end;

procedure TForm1.WmAfterShow(var Msg: TMessage);
begin
  Telnet := TTelnet.Create(Self);
  with Telnet do
  begin
    ConsoleText := Memo1.Lines;
    Host := '192.168.100';  //需改为实际ip或计算机名
    Port := 23;  //需改为实际端口
    Connect;
    if not Connected then
      ShowMessage('Not Connected.')
    else if not Login('administrator', 'passwordxx') then //需改为实际用户和密码
       ShowMessage('Login Failed.');
  end;
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
  Memo1.SetFocus;
  Memo1.SelStart := Length(Memo1.Lines.Text);
  SendMessage(Memo1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
  Application.ProcessMessages;
  Edit1.SetFocus;
end;

procedure TForm1.ButtonSendClick(Sender: TObject);
begin
  ButtonSend.Enabled := False;
  Edit1.Enabled := False;
  Telnet.SendCmd(Edit1.Text);
  //Edit1.Text := '';
  ButtonSend.Enabled := True;
  Edit1.Enabled := True;

  Application.ProcessMessages;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    ButtonSendClick(nil);
  end;
end;

end.

Unit1.dfm: 

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 508
  ClientWidth = 663
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnShow = FormShow
  DesignSize = (
    663
    508)
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TRichEdit
    Left = 8
    Top = 8
    Width = 647
    Height = 451
    Anchors = [akLeft, akTop, akRight, akBottom]
    Font.Charset = GB2312_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    ScrollBars = ssVertical
    TabOrder = 0
    Zoom = 100
    OnChange = Memo1Change
  end
  object Edit1: TEdit
    Left = 176
    Top = 473
    Width = 193
    Height = 21
    Anchors = [akLeft, akBottom]
    TabOrder = 1
    Text = 'dir'
    OnKeyPress = Edit1KeyPress
  end
  object ButtonSend: TButton
    Left = 384
    Top = 471
    Width = 75
    Height = 25
    Anchors = [akLeft, akBottom]
    Caption = #25191#34892
    TabOrder = 2
    OnClick = ButtonSendClick
  end
end

Unit2.pas: 

unit Unit2;

interface

uses Vcl.Forms, System.SyncObjs, System.Classes, System.SysUtils,
     IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdTelnet,
     IdGlobal;

type
  TTelnet = class(TIdTelnet)
  private
    LastStr: String;
    function WaitFor(S: String; TimeOut: Integer = 10000): Boolean; overload;
    function WaitFor(C: Char  ; TimeOut: Integer = 10000): Boolean; overload;
    procedure TelnetDataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
  public
    ConsoleText: TStrings;
    function Login(aUser, aPassword: String): Boolean;
    function SendCmd(const S: String): Boolean;
    constructor Create(AOwner: TComponent = nil);
  end;

implementation

const
  CRLF = #13#10;

constructor TTelnet.Create(AOwner: TComponent);
begin
  inherited;
  OnDataAvailable := TelnetDataAvailable;
  ThreadedEvent := False;
  LastStr := '';
end;

function TTelnet.SendCmd(const S: String): Boolean;
begin
  SendString(S + CRLF);
  Result := WaitFor('>');
end;

function TTelnet.Login(aUser, aPassword: String): Boolean;
begin
  Result := False;
  if not WaitFor('login: ', 15000) then Exit;

  SendString(aUser + CRLF);
  if not WaitFor('password: ') then Exit;

  SendString(aPassword + CRLF);
  if not WaitFor('>') then Exit;

  Result := True;
end;

function TTelnet.WaitFor(S: String; TimeOut: Integer): Boolean;
var
  I, Count: Integer;
begin
  Result := False;
  Count := TimeOut div 25;
  for I := 1 to Count do
  begin
    if LastStr = S then
    begin
      Result := True;
      Exit;
    end;
    Sleep(25);
    Application.ProcessMessages;
  end;
end;

function TTelnet.WaitFor(C: Char; TimeOut: Integer): Boolean;
var
  I, Count: Integer;
begin
  Result := False;
  Count := TimeOut div 25;
  for I := 1 to Count do
  begin
    if LastStr[LastStr.Length] = C then
    begin
      Result := True;
      Exit;
    end;
    Sleep(25);
    Application.ProcessMessages;
  end;
end;

procedure TTelnet.TelnetDataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
var
  S: String;
  Count, I, P: Integer;
begin
  S := TEncoding.ANSI.GetString(Buffer);
  Count := ConsoleText.Count;
  if Count = 0 then
     ConsoleText.Add(S)
  else
    ConsoleText[Count-1] := ConsoleText[Count-1] + S;

  P := 0;
  for I := S.Length Downto 1 do
  begin
    if S[I] = #13 then
    begin
      P := I;
      if (P < S.Length) and (S[P+1] = #10) then Inc(P);
      Break;
    end;
  end;

  if P > 0 then
  begin
    LastStr := Copy(S, P+1, S.Length);
  end
  else
  begin
    LastStr := LastStr + S;
  end;
end;

end.

 

使用TIdAntiFreeze对抗“冻结”   Indy使用一个特殊的组件TIdAntiFreeze来透明地解决客户程序用户界面“冻结”的问题。TIdAntiFreeze在Indy内部定时中断对栈的调用,并在中断期间调用Application.ProcessMessages方法处理消息,而外部的Indy调用继续保存阻塞状态,就好像TIdAntiFreeze对象不存在一样。你只要在程序中的任意地方添加一个TIdAntiFreeze对象,就能在客户程序中利用到阻塞式Socket的所有优点而避开它的一些显著缺点。   Indy使用了线程技术   阻塞式Socekt通常都采用线程技术,Indy也是如此。从最底层开始,Indy的设计都是线程化的。因此用Indy创建服务器和客户程序跟在Unix下十分相似,并且Delphi的快速开发环境和Indy对WinSock的良好封装使得应用程序创建更加容易。   Indy服务器模型   一个典型的Unix服务器有一个或多个监听进程,它们不停地监听进入的客户连接请求。对于每一个需要服务的客户,都fork一个新进程来处理该客户的所有事务。这样一个进程只处理一个客户连接,编程就变得十分容易。   Indy服务器工作原理同Unix服务器十分类似,只是Windows不像Unix那样支持fork,而是支持线程,因此Indy服务器为每一个客户连接分配一个线程。   图1显示了Indy服务器的工作原理。Indy服务器组件创建一个同应用程序主线程分离的监听线程来监听客户连接请求,对于接受的每一个客户,都创建一个新的线程来为该客户提供服务,所有与这一客户相关的事务都由该线程来处理。   使用组件TIdThreadMgrPool,Indy还支持线程池。   线程与Indy客户程序   Indy客户端组件并未使用线程。但是在一些高级的客户程序中,程序员可以在自定义的线程中使用Indy客户端组件,以使用户界面更加友好。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值