Telnet:下次读取时先前写过的抓取字节? [英] Telnet: Grabbing Bytes Previously Written During Next Read?

查看:72
本文介绍了Telnet:下次读取时先前写过的抓取字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我正在建立一个自定义的telnet界面,我的问题是我想要读取用户输入以及之前编写的流。


现在我正在登录用户。


我有


登录:Bill


登录记录:


buffer = ASCII.GetBytes(" Login:");

_clientStream.Write(buffer,0,buffer.Length);


Bill由用户输入。


下次返回时,我想阅读:


登录:比尔。


这就是我知道它的登录阅读方式

而不是密码:阅读。


我正在阅读以下循环的输入:


代码:

while(true)

{


_bytesRead = 0;

try

{

//阻塞直到客户端发送消息

_bytesRead = _clientStream.Read(消息,

0,4096); //消息


}

catch

{

//发生套接字错误

休息;

}


if(_bytesRead == 0)

{

休息;

}


statusMessage + = ASCII.GetString(消息,0,

_bytesRead);

但是,我的问题是我得到:


" Bill"


当用户提交他的时候字符串。


如何同时获得书面和登录:并且

阅读Bill在同一个阅读?


如果我需要提供更多信息,请告诉我,

otherwsie,非常感谢您的回复!

解决方案

2008年1月16日星期三09:57:27 -0800,pbd22< du ***** @ gmail.comwrote:
< blockquote class =post_quotes>
[...]

下次回复时,我想读:


"登录:比尔。


这就是我知道它的登录阅读

而不是密码:阅读。


[...]

但是,我的问题是我得到:


" Bill"


当用户提交他的字符串时。


如何同时获得书面的登录:并且

阅读Bill在同一读?



也许它已经太长了,而且我错误地记得telnet协议如何工作,但我的回忆是, telnet客户端不应该

回显你发送的数据。


换句话说,永远不会有登录:为您的telnet服务器

读取。您需要明确跟踪连接的状态,通过

知道您发送给客户端的内容,因为它与您收到的输入相关

收到返回。


例如:假设您确定自己已经处理了所有用户输入,最多为

这一点(对于登录,这应该是微不足道的:)),然后当你发送一个

登录提示时,你需要设置一些本地状态,以便你知道你应该从用户收到的下一个
的东西是登录ID。


一旦你成功处理了用户的登录ID响应,那么

你会发送密码提示并设置一些本地状态,以便你知道你应该从用户那里收到的下一件事是登录密码..


状态可以像描述客户连接的各个阶段一样简单:


enum ClientState

{

LoginIDPrompt,

LoginPasswordPrompt,

已连接

}


然后是switch()语句,其中处理用户输入,该用户输入使用与分配给枚举值的客户端关联的

变量,

决定如何处理输入:


//此时,已经阅读了整行客户端

//输入并将其存储在变量中(例如,strUserInput

//为了讨论的缘故):

switch(clientData.ClientState)

{

case ClientState.LoginIDPrompt :

strLoginID = strUserInput;

clientData.ClientState = ClientState.LoginPasswordPrompt;

break;

case ClientState。 LoginPasswordPrompt;

if(CheckPassword(strLoginID,strUserInput))

{

clientData.ClientState = ClientState.Connected;

}

else

{

SendLoginPrompt();

clientData.ClientState = ClientState.LoginIDPrompt;

}

break;

case ClientState.Connected:

//在这里处理常规用户输入

休息;

}


这只是基本想法。有很多方法可以实际实现

吧。


Pete


谢谢Pete。


我正在通过你的代码。

你能解释一下clientData是什么吗? clientData.ClientState

是什么?


对不起 - 不太熟悉枚举。


谢谢,

彼得


谢谢皮特。


我正在通过你的代码。

你能解释一下clientData是什么吗? clientData.ClientState

是什么?


对不起 - 不太熟悉枚举。


谢谢,

彼得


Hi.

I am building a custom telnet interface and my problem is that I
want to read the user input along with the previously written stream.

Right now I am logging the user.

I have

Login: Bill

Login is written by:

buffer = ASCII.GetBytes("Login: ");
_clientStream.Write(buffer, 0, buffer.Length);

Bill is entered by the user.

At the next return, I want to read:

"Login: Bill".

This is how I know its a Login read
and not a Password: read.

I am reading input with the following loop:

Code:
while (true)
{

_bytesRead = 0;
try
{
//blocks until a client sends a message
_bytesRead = _clientStream.Read(message,
0,4096); //message

}
catch
{
//a socket error has occured
break;
}

if (_bytesRead == 0)
{
break;
}

statusMessage += ASCII.GetString(message, 0,
_bytesRead);
But, my problem is that I get:

"Bill".

when the user submits his string.

How do I get both the written "Login: " and
the read "Bill" on the same read?

Please let me know if i need to provide more information,
otherwsie, thanks a lot for your response!

解决方案

On Wed, 16 Jan 2008 09:57:27 -0800, pbd22 <du*****@gmail.comwrote:

[...]
At the next return, I want to read:

"Login: Bill".

This is how I know its a Login read
and not a Password: read.

[...]
But, my problem is that I get:

"Bill".

when the user submits his string.

How do I get both the written "Login: " and
the read "Bill" on the same read?

Maybe it''s been too long and I''m misremembering how the telnet protocol
works, but my recollection is that the telnet client is not supposed to
echo the data you send it.

In other words, there will never be a "Login: " for your telnet server to
read. You need to track the state of the connection explicitly, by
knowing what you''ve sent to the client, as it relates to what input you''ve
received back.

For example: assuming you are sure you''ve processed all user input up to
this point (for a login, this should be trivial :) ), then when you senda
login prompt, you need to set some local state so that you know the next
thing you should receive from the user is the login ID.

Once you have successfully processed the user''s login ID response, then
you would send the password prompt and set some local state so that you
know the next thing you should receive from the user is a login password..

The "state" could as simple as an enum that describes the various stages
of a client connection:

enum ClientState
{
LoginIDPrompt,
LoginPasswordPrompt,
Connected
}

and then a switch() statement where you process user input that uses a
variable associated with the client assigned to a value from the enum to
decide what to do with the input:

// at this point, having read an entire line of client
// input and stored it in a variable (say, "strUserInput"
// for the sake of discussion):
switch (clientData.ClientState)
{
case ClientState.LoginIDPrompt:
strLoginID = strUserInput;
clientData.ClientState = ClientState.LoginPasswordPrompt;
break;
case ClientState.LoginPasswordPrompt;
if (CheckPassword(strLoginID, strUserInput))
{
clientData.ClientState = ClientState.Connected;
}
else
{
SendLoginPrompt();
clientData.ClientState = ClientState.LoginIDPrompt;
}
break;
case ClientState.Connected:
// handle regular user input here
break;
}

That''s just the basic idea. There are lots of ways to actually implement
it.

Pete


Thanks Pete.

I am making my way though your code.
Could you explain what the "clientData" of "clientData.ClientState"
is?

Sorry - not too familiar with enums.

Thanks,
Peter


Thanks Pete.

I am making my way though your code.
Could you explain what the "clientData" of "clientData.ClientState"
is?

Sorry - not too familiar with enums.

Thanks,
Peter


这篇关于Telnet:下次读取时先前写过的抓取字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆