拖入Indy Client集合里面的IdTelnet控件,在属性里面设好host和port.之后下面一段示例代码
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdTelnet;
type
TForm1 = class(TForm)
Button1: TButton;
IdTelnet1: TIdTelnet;
Button3: TButton;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure onCloseClick(Sender: TObject; var Action: TCloseAction);
procedure myDataAvailable(Sender: TIdTelnet; const Buffer: String);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if( not IdTelnet1.Connected ) then
begin
IdTelnet1.Connect;
end
end;
procedure TForm1.myDataAvailable(Sender: TIdTelnet; const Buffer: String);
begin
Memo1.Text := Memo1.Text + '\n' + pchar(Buffer);
//定位Memo到底部,显示最新输入
SendMessage(memo1.Handle,WM_VSCROLL,SB_BOTTOM,0);
end;
procedure TForm1.onCloseClick(Sender: TObject; var Action: TCloseAction);
begin
IdTelnet1.Disconnect;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
b:String;
i:Integer;
begin
if( IdTelnet1.Connected ) then
begin
b:= Edit1.Text;
Edit1.Text := '';
//这里换成SendCmd之后会死在这,未找到解决办法
for i:=1 to Length(b) do
IdTelnet1.SendCh(b[i]);
IdTelnet1.SendCh(#13);
end
else
begin
Application.MessageBox('连接已断开,请重新连接','系统提示');
end;
end;
end.
在调试模式下当建立连接后关闭程序(当中调用了IdTelnet1.Disconnect)会报错
***EAccessViolation***'Access violation at***
但是运行编译出来的exe执行文件错误并不会显示,貌似不影响程序使用,未找到解决办法.