473,785 Members | 2,468 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 #1
22 1879
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
Jan 16 '08 #2
Thanks Pete.

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

Sorry - not too familiar with enums.

Thanks,
Peter
Jan 16 '08 #3
Thanks Pete.

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

Sorry - not too familiar with enums.

Thanks,
Peter
Jan 16 '08 #4
Thanks Pete.

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

Sorry - not too familiar with enums.

Thanks,
Peter
Jan 16 '08 #5
Thanks Pete.

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

Sorry - not too familiar with enums.

Thanks,
Peter
Jan 16 '08 #6
On Wed, 16 Jan 2008 10:43:33 -0800, pbd22 <du*****@gmail. comwrote:
Thanks Pete.

I am making my way though your code.
Could you explain what the "clientData " of "clientData.Cli entState"
is?
"clientData " is just a variable name I made up. You should have in your
own code some specific variable that contains state information about each
client connection (such as the _clientStream variable). You may in fact
put code similar to what I posted _inside_ the class that contains this
state (the fact that the _clientStream variable is unqualified suggests
you may already be doing this), in which case you wouldn't need the
"clientData " part at all. You'd just refer to the "ClientStat e" member
directly (or whatever you choose to call it).
Sorry - not too familiar with enums.
I apologize for any confusion. The "clientData " doesn't really have
anything to do with the enum aspect. The "ClientStat e" is simply a member
of a data structure (probably class, but could be a struct instead) that
has the type ClientState, and "clientData " would simply be a reference to
an instance of that data structure.

For that matter, don't get too hung up on the enum. I used an enum
because it's a convenient, readable way to describe simple state like
this, but you can manage the state however you like.

Pete
Jan 16 '08 #7
Yes, I think I am already handling state information at the head of my
TheConnectionHa ndler
with the following lines of code:

_telnetSocket = (TcpClient)conn ectionQueue.Deq ueue();
_clientStream = _telnetSocket.G etStream();
Here is the core of the handler and the Login method per your
suggestion. Does this look right?

Thanks again.

public void TheConnectionHa ndler()
{

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

//if (IsLoggedIn == false)
//{
// 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)
{

buffer = ASCII.GetBytes( Login(statusMes sage));

}
}

_telnetSocket.C lose();

}

protected string Login(string clientInput)
{

switch (ClientState)
{
case ClientState.Log inIDPrompt:
strLoginID = clientInput;
clientData.Clie ntState =
ClientState.Log inPasswordPromp t;
statusMessage = "";
break;
case ClientState.Log inPasswordPromp t:
if (CheckPassword( strLoginID, clientInput))
{
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;
}
Jan 16 '08 #8
On Wed, 16 Jan 2008 11:36:52 -0800, pbd22 <du*****@gmail. comwrote:
Yes, I think I am already handling state information at the head of my
TheConnectionHa ndler
with the following lines of code:

_telnetSocket = (TcpClient)conn ectionQueue.Deq ueue();
_clientStream = _telnetSocket.G etStream();
Here is the core of the handler and the Login method per your
suggestion. Does this look right?
Mostly. Some suggestions:

* Don't have the Login() method manage the "statusMess age" variable..
Your "TheConnectionH andler()" method is a more appropriate place to do
that, as it's what is manipulating it otherwise.

* You should use the index of the EOL for your user input to createa
substring that's passed to the Login() method, to ensure that that method
gets _only_ the one line of user input. TCP being what it is, it's
possible to receive additional data after the EOL as part of the same
chunk of read data. In the user login scenario hopefully the chances are
less (presuambly it'd require the client to type ahead, entering both the
login ID and the password all at once, before any of that data actually
gets sent...not impossible, but probably unlikely). But you shouldn't
leave something like that to chance.

* And combining the two above, obviously this means that you shouldn't
just clear the "statusMess age" variable to an empty string. You should
extract the data you're ready to process, and reassign the remaining data
to the variable (so basically two calls to Substring() to split up the
string).

Of course, you left in the "clientData " variable from my original
suggestion...if you're just storing a ClientState field or property, then
anywhere you've written "clientData.Cli entState", you probably just want
"ClientStat e". I assume that's just an oversight that would be addressed
in the actual code (since it wouldn't compile otherwise).

Pete
Jan 16 '08 #9
Thanks Pete.

I feel a bit thick on this.

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".

When I do something like

ClientSate state = new ClientState();

or

state = new ClientState();

and then

switch (ClientState)

the compiler still throws the error.

shouldn't i be instantiating the connection
as a new ClientSate? Or something like that?

What am I not getting?

ps -

I have put:

enum ClientState
{
LoginIDPrompt,
LoginPasswordPr ompt,
Connected
}

inside my class next to my
variable declarations.
Jan 16 '08 #10

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
14099
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
2107
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
2997
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
7348
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
10357
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
10163
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...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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...
1
4063
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
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.