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,
LoginPasswordPr ompt,
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, "strUserInp ut"
// for the sake of discussion):
switch (clientData.Cli entState)
{
case ClientState.Log inIDPrompt:
strLoginID = strUserInput;
clientData.Clie ntState = ClientState.Log inPasswordPromp t;
break;
case ClientState.Log inPasswordPromp t;
if (CheckPassword( strLoginID, strUserInput))
{
clientData.Clie ntState = ClientState.Con nected;
}
else
{
SendLoginPrompt ();
clientData.Clie ntState = ClientState.Log inIDPrompt;
}
break;
case ClientState.Con nected:
// handle regular user input here
break;
}
That's just the basic idea. There are lots of ways to actually implement
it.
Pete