On Wed, 16 Jan 2008 14:04:23 -0800, pbd22 <du*****@gmail. comwrote:
Hey Peter,
OK, I am starting to get the swing of things, thanks.
The last bit I am having probs with is the 2 substrings.
My current program is going screwy once it connects.
It either hangs, or says your connected after I type each
letter in the Password: field.
I think it has to do with the statusMessage variable.
I think this last bit will do it.
Well, this is related to my suggestion that you only manage the
statusMessage variable from within the method "TheConnectionH andler()".
The code you put in the Login() method to deal with updating the
statusMessage variable is wrong in any case (IndexOf() and LastIndexOf()
in that statement are both returning the same index...nothing 's really
getting removed from the string), but IMHO the code will be easier to
understand and fix if you put that statement in the method dealing with
i/o, rather than the one dealing with login credentials.
IMHO, you should remove the line that attempts to deal with updating the
statusMessage variable from the Login() method. Then the section of code
in TheConnectionHa ndler() that deals with the data after it's been
converted to a string should look more like this:
// You want the first EOL in the string, not the last.
int ichEOL = statusMessage.I ndexOf(ENDOFLIN E);
// If there is an EOL character, then process the current line
if (ichEOL >= 0)
{
// Extract the line of text, up to the EOL (but not including)
string strLine = statusMessage.S ubstring(0, ichEOL++);
// If there are characters after the EOL, set the current
// string to those characters. Otherwise, just reset it to
// an empty string.
if (ichEOL < statusMessage.L ength)
{
statusMessage = statusMessage.S ubstring(ichEOL );
}
else
{
statusMessage = "";
}
if (currentState != ClientState.Con nected)
{
Login(strLine);
}
else
{
buffer = ASCII.GetBytes( "You are connected!");
_clientStream.W rite(buffer, 0, buffer.Length);
_clientStream.F lush();
}
}
Now, all that said, I'll note that you haven't really got a genuine telnet
implementation there. As you saw when dealing with the entering of the
password, data is sent as the user types it. It's not being sent a line
at a time, rather it's being sent a character at a time.
As long as the data is entered without errors, the above should work
fine. But as soon as you have a situation where the user wants to
backspace, you've got a problem. The backspace characters are just going
to get added to the input string, and so of course when the user finally
does send the EOL character, the string up to that point will be messed up.
IMHO, you would be better off refactoring the code so that you've got two
layers: a low-level telnet i/o layer that can deal with backspaces and the
line, and a higher level layer that deals only in complete lines. The
lower level wouldn't present a complete line to the higher level until it
sees an EOL character.
Whether you split the code up like that or not, you definitely need to
include some logic to deal with the user entering backspaces (and possibly
other control characters as well).
Pete