我从一个vb的控件中改编了一些代码,用于分析telnet返回的信息。
程序如下,但却不能入我所想的去工作,请各位帮忙指正。
type
TfrmMain = class(TForm)
csTelnet: TClientSocket;
btnConnect: TButton;
btnDisconnect: TButton;
mmoInformation: TRichEdit;
procedure btnConnectClick(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure csTelnetRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure mmoInformationKeyPress(Sender: TObject;
var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses termUnit;
{$R *.DFM}
procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
cstelnet.Open;
{port:=23;address:=202.112.14.174 电子科技大学的 bbs}
end;
procedure TfrmMain.btnDisconnectClick(Sender: TObject);
begin
csTelnet.Close;
end;
procedure TfrmMain.csTelnetRead(Sender: TObject;
Socket: TCustomWinSocket);
var sOutput:string;
begin
sOutput:=HandleData(Socket);
showmessage(soutput);//这里显示的是空白内容;
If Length(sOutput) > 0 then
begin
mmoInformation.SelStart :=65535;
mmoInformation.SelLength :=0;
mmoInformation.seltext:=sOutput;
end;
end;
procedure TfrmMain.mmoInformationKeyPress(Sender: TObject;
var Key: Char);
begin
csTelnet.Socket.SendText(key);
end;
---------------
unit termUnit;
interface
uses scktComp,dialogs;
Function HandleData(Socket:TCustomWinSocket):String;
implementation
uses termConst;
Function HandleData(Socket:TCustomWinSocket):String;
var
sBuffer, sOutput , sReply : String;
nRead , nIndex , nChar : Integer;
nCmd , nOpt , nQual : Integer;
begin
sBuffer:=Socket.ReceiveText;
showmessage(sbuffer);//这里也显示空白内容,而且只显示了一次。
nRead:=Socket.ReceiveLength ;
nIndex := 1;
While nIndex <= nReaddo
begin
nChar := ord(sBuffer[nIndex]);
//如果是命令标志字符IAC(Is A command),那么下一个就是命令
If nChar = TELCMD_IAC then
begin
nIndex := nIndex + 1;
nCmd := ord(sBuffer[nIndex]);
Case nCmd of
// 两个IAC字符表示不是一个真正的命令
TELCMD_IAC: sOutput:= sOutput + chr(nChar);
// 子项命令SB(sub-option command)告诉我们服务器要交涉(negotiate)
//在这种情况下,我们仅能处理的终端类型子项.
TELCMD_SB:
begin
nIndex := nIndex + 1;
nOpt := ord(sBuffer[nIndex]);
nIndex := nIndex + 1;
nQual := ord(sBuffer[nIndex] );
If nOpt = TELOPT_TTYPE then
begin
// 建立一个子项回答字符并把它发给服务器
//在这种情况下,我们说我们是DEC VT100 终端
sReply := chr(TELCMD_IAC) + chr(TELCMD_SB)+ chr(nOpt)
+ chr(TELQUAL_IS)+ 'DEC-VT100' + chr(TELCMD_IAC)
+ chr(TELCMD_SE);
Socket.SendText(sReply);
end;
end;
// 从服务器发来的命令DO,do
NT, WILL and WONT 告诉我们它是(不)兼容的
//选项是我们愿意使用的,下一个字符是选项代码
TELCMD_DO, TELCMD_DONT, TELCMD_WILL, TELCMD_WONT:
begin
nIndex:= nIndex + 1;
nOpt := ord(sBuffer[nIndex]);
Case nOpt of
// 我们仅能处理的选项是echo和terminal类型的
TELOPT_BINARY, TELOPT_ECHO, TELOPT_TTYPE:
if nCmd = TELCMD_DO then
begin
sReply := chr(TELCMD_IAC) + Chr(TELCMD_WILL) + Chr(nOpt);
Socket.SendText (sReply);
End
else
if ncmd=TELCMD_WILL then
begin
sReply := Chr(TELCMD_IAC) + Chr(TELCMD_DO) + Chr(nOpt);
Socket.SendText(sReply);
end;
// 对于其他情况,告诉服务器我们不支持
else
{Case else
}
If nCmd = TELCMD_DO then
begin
sReply := Chr(TELCMD_IAC) + Chr(TELCMD_WONT) + Chr(nOpt);
Socket.SendText (sReply);
End
else
If nCmd = TELCMD_WILL then
begin
sReply := Chr(TELCMD_IAC) + Chr(TELCMD_DONT) + Chr(nOpt);
Socket.Sendtext(sReply);
end;
end;
{case nopt}
end;
{select TELCMD_DO, TELCMD_DONT, TELCMD_WILL, TELCMD_WONT}
end;
{Case ncmd}
End{End if}
else
sOutput := sOutput + Chr(nChar);
nIndex:= nIndex + 1;
end;
{While}
Result:=sOutPut;
// 把输出结果送去显示
{
If Length(sOutput) > 0 then
begin
mmoInformation.SelStart :=65535;
mmoInformation.SelLength :=0;
mmoInformation.seltext:=sOutput;
end;
}
end;
end.