473,799 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Telnet: Grabbing Bytes Previously Written During Next Read?

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.W rite(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.R ead(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!
Jan 16 '08
22 1880
On Wed, 16 Jan 2008 12:25:27 -0800, pbd22 <du*****@gmail. comwrote:
Thanks Pete.

I feel a bit thick on this.
That's okay. I know how hard it can be to try to write code on a Monday..
:)
I am still struggling with ClientSate in the switch statement.

Left alone, I get

"ClientStat e is a type but is used like a variable".
That's because you (apparently) do not have a variable named "ClientStat e".
When I do something like

ClientSate state = new ClientState();
Variable name is "state", not "ClientStat e".
or

state = new ClientState();
See above.
and then

switch (ClientState)

the compiler still throws the error.
Sure, it would. If you've named your variable "state" instead of
"ClientStat e", then the switch statement should read "switch (state)"
instead of "switch (ClientState)".

Again, this isn't unique to enums...don't get hung up the fact that it's
an enum. The point of using an enum is that they can be used like a
regular simple value type. So the rules for writing code using enums are
the same as if you were writing code using, for example, an int or a char
or a long, etc.

I'm sorry if using the same name for the variable as the type has confused
things. It's funny...I generally try _not_ to do that, but it's a common
..NET convention for naming properties and public fields. So when I had a
data member of a data structure of type "ClientStat e" in my original
example, I just reused the type name for the name of that data member.
Things seem to have gone downhill from there. :)

Pete
Jan 16 '08 #11
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.

Thanks again.
Peter
public void TheConnectionHa ndler()
{

_telnetSocket = (TcpClient)conn ectionQueue.Deq ueue();
_clientStream = _telnetSocket.G etStream();

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

while (true)
{

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

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

if (_bytesRead == 0)
{
break;
}

statusMessage += ASCII.GetString (message, 0,
_bytesRead);

if (statusMessage. LastIndexOf(END OFLINE) 0)
{

if (currentState != ClientState.Con nected)
{
Login(statusMes sage.Substring( 0,
statusMessage.I ndexOf(ENDOFLIN E)));
//buffer = ASCII.GetBytes( statusMessage);
}
else
{
buffer = ASCII.GetBytes( "You are connected!");
_clientStream.W rite(buffer, 0, buffer.Length);
_clientStream.F lush();

}
}

}

_telnetSocket.C lose();

}

protected void Login(string clientInput)
{

switch (currentState)
{
case ClientState.Log inIDPrompt:
strLoginID = clientInput;
currentState = ClientState.Log inPasswordPromp t;
statusMessage =
statusMessage.R emove(statusMes sage.IndexOf(cl ientInput),
statusMessage.L astIndexOf(clie ntInput)); < --- SHOULD I DO THIS HERE?
//statusMessage = "";
SendPrompt();
break;
case ClientState.Log inPasswordPromp t:
if (CheckPassword( strLoginID, clientInput))
{
currentState = ClientState.Con nected;
}
else
{
currentState = ClientState.Log inIDPrompt;
}
break;
case ClientState.Con nected:
{
break;
}
}

}

private void SendPrompt()
{
buffer = ASCII.GetBytes( "Password: ");
_clientStream.W rite(buffer, 0, buffer.Length);
_clientStream.F lush();

}

public static bool CheckPassword(s tring name, string pass)
{

if (name == "First" && pass == "Last")
{
return true; // just for kicks. remove it.
}
else
{
return false;
}

}
Jan 16 '08 #12
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
Jan 16 '08 #13
Pete -

Thanks,

I agree with you about the "character-by-character" implementation.
When I enter my password things get screwy. I need to figure this
out.

Your code sort of worked. I get all sorts of garbage when I get to the
password ("/n") or ("/r") or ("r/n/r"). These are usually before the
actual
password. I think it may have to do with the fact that the ENDOFLINE
variable (that you can't see) is actually

const string ENDOFLINE = "\r\n";

Maybe this is messing things up?

Peter
Jan 16 '08 #14
Pete -

Thanks,

I agree with you about the "character-by-character" implementation.
When I enter my password things get screwy. I need to figure this
out.

Your code sort of worked. I get all sorts of garbage when I get to the
password ("/n") or ("/r") or ("r/n/r"). These are usually before the
actual
password. I think it may have to do with the fact that the ENDOFLINE
variable (that you can't see) is actually

const string ENDOFLINE = "\r\n";

Maybe this is messing things up?

Peter
Jan 16 '08 #15
OK,

You are right. I need to figure out how to get the Password line to
react to lines and not characters...

Jan 16 '08 #16
OK,

You are right. I need to figure out how to get the Password line to
react to lines and not characters...

Jan 16 '08 #17
On Wed, 16 Jan 2008 15:16:01 -0800, pbd22 <du*****@gmail. comwrote:
[...]
I think it may have to do with the fact that the ENDOFLINE
variable (that you can't see) is actually

const string ENDOFLINE = "\r\n";

Maybe this is messing things up?
Sure, that's definitely going to be part of it. I didn't realize that was
a string instead of a character and the code I posted assumes it's a
single character. That's easy enough to change though. Just change the
"ichEOL + 1" to "ichEOL + EOL.Length".

As you've noted, there are other things you'll need to address as well.
But that should at least fix that issue. :)

Pete
Jan 17 '08 #18
Thanks Pete.

I am getting there thanks to your help.

I have some nick-knacks that are, it seems,
not that trivial.

1) how do I highlight a line?
2) how do I change background color?
3) how do I change font color?
4) how do I clear the screen (aka. DOS "cls");

For anybody who cares to tackle 'em...

Thanks again for your help,
Peter

Jan 17 '08 #19
One more to the list for anybody who might know...

1) how do I highlight a line?
2) how do I change background color?
3) how do I change font color?
4) how do I clear the screen (aka. DOS "cls");
5) how to hide password entry (as clear-text or XXXXXXX)?

Thanks again,
Peter
Jan 17 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8664
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function seems to freeze after a couple of command. I did a simplify script that reproduce the problem each time (I'm using 2.3.4 on W2K):
7
14102
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a Telnet client. I just need to send a telnet request to a local IP address on a LAN issue a "c" then a "b" and stream back the text for internal use. The "c" changes sub-menus and the "b" is a switch to dump the status of a firewall. I need to issue...
0
3860
by: goroth | last post by:
I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to get the correct return data from the device. Sometimes I get about 100 bytes and other times I get 3000 bytes. I even put a thread sleep but that still give me different return data from different devices. I know I am going to have to use an...
4
2958
by: Patricia Mindanao | last post by:
I want to call cgi perl scripts on my web hosters server from my HTML web pages (on the the web hosters server too). It occurs sometimes (especially during development phase) that these cgi-perl scripts didn't work like intended. They crash because e.g. - syntax errors - wrong or changing pathes - unexpected user input - ...
1
2109
by: Jia Lu | last post by:
Hello I have a program that can telnet to a host. But I cannot understand from part, can anyone explain it to me? Thank you very much. import sys, posix, time
6
6156
by: =?Utf-8?B?U2hhcmllZg==?= | last post by:
Dear All, I must write a client program in C# which will communicate with a switch throught telnet. When I create a socket connection on port 22, the switch responds with some text and at the end with some unreadable characters. I found out that the operating system of the switch is redhat and the protocol = SSH-2.0-openssh_3.9p1. My problem:
0
191
by: pbd22 | last post by:
Hi. I have Login: Bill Login is written by my program. Bill is entered by the user. At the next return, I want to read:
2
2998
by: thamayanthi | last post by:
Hi, The below code is used to connect to the remote machine using Telnet module in perl. use Net::Telnet; use Net::Ping; $telnet = new Net::Telnet (Timeout=>10,Errmode=>'die'); open (FILE,"C:/start.txt") or die ("Unable to open start.txt"); while(<FILE>)
17
7349
by: ravimath | last post by:
Dear all, I have written following script to loin to router bu it is showing error. #!c:\Perl\bin; use strict; use warnings; my $hostname = 'REMOVED FOR YOUR PROTECTION'; my $password = 'REMOVED FOR YOUR PROTECTION';
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.