473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom Telnet Windows Service

Hi

I am making my way through a windows service that accepts
custom telnet commands as strings and then parses the string
and redirects it.

This would be a custom string sent via telnet.

So, I need to prompt the user for input and then capture
that imput, parse it into a form that is readable (strings in
variables) and then send it somewhere (to a database or
xml).

What I have right now hijacks the telnet connection by
spawning a tread and queuing connections, but I am
still unclear how to send custom prompts to the user
and capture responses.

Also, I need a login (username/password) before we can
start.

Below is my code. Could somebody walk me through
how to add the interactivity?

Much appreciated!
protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
new Thread(new ThreadStart(Listen)).Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
this.timer1.Enabled = false;
this.Listen(); // stop / start
}

private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
this.Listen();
}

public void Listen()
{

eventLog2.WriteEntry("Listener Service Started...",
EventLogEntryType.Information);

try
{
Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized(unsyncq);
}
catch( Exception e)
{
eventLog2.WriteEntry( "Error in Listener method ( queue
related ) : " + e.Message, EventLogEntryType.Error);
return;
}

try
{

TcpClient socket;
TcpListener listener = new TcpListener(IPAddress.Any,
23);

listener.Start();

while( true)
{
socket = listener.AcceptTcpClient();
connectionQueue.Enqueue(socket);

Thread workingthread = new Thread(new
ThreadStart(TheConnectionHandler));
workingthread.Start(socket);
}

}
catch( Exception e)
{
eventLog2.WriteEntry( "Error in Listener method
( connection related ) :" + e.Message, EventLogEntryType.Error);
return;
}

}

public void TheConnectionHandler()
{

TcpClient socket =
(TcpClient)connectionQueue.Dequeue();

byte[] message = new byte[4096];
int bytesRead;

while (true)
{

bytesRead = 0;

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

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

if (bytesRead == 0)
{

//the client has disconnected from the server
break;

}

//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(
encoder.GetString(message, 0, bytesRead));

}

socket.Close();

}
Jan 14 '08 #1
4 2842
Hi.

I'm assuming everything's worked until now and you're getting what the
user types on the server side dumped to the destination of your Debug.
Here's where you need to do something:
* * * * * * * * * * * * //message has successfullybeen received
* * * * * * * * * * * * ASCIIEncoding encoder = new ASCIIEncoding();
* * * * * * * * * * * * System.Diagnostics.Debug.WriteLine(
* * * * * * * * * * * * encoder.GetString(message,0, bytesRead));
You have a string -- encoder.GetString() gives you one -- so parse it
then return the proper response. You may try something like this:

protected string Response(string input)
{
if (input == "hello")
return "hello, back.";
}

Then you need to take the contents of the string you get back and push
it back into your stream. clientStream.Write() is a good place to
look.

If you want to do prompts, you should push out a prompt as soon as you
make the connection, before first listening for a response. Some
servers expect some input before the first prompt (many telnet
implementations expect a CR from the client before they provide the
first prompt). Then prompt wish "Username: " and read the response.
When the user this enter (you get a CR character), prompt "Password: "
and read the response. Check the username and password provided
against your lookup. If it's invalid, go back to the start. If it's
valid, provide the first 'do something' prompt.

HTH!

s}
Jan 14 '08 #2
Thanks.

Do I pass "encoder" as an argument to Response?
If this is wrong, would you mind providing a bit more code to give me
a better understanding?

Thanks
Jan 14 '08 #3
On Jan 14, 4:16*pm, pbd22 <dush...@gmail.comwrote:
Thanks.

Do I pass "encoder" as an argument to Response?
If this is wrong, would you mind providing a bit more code to give me
a better understanding?

Thanks
No, you pass the output of encoder.GetString(). When you get data from
the TCP/IP stream, you're assuming they're ASCII-encoded bytes. In any
case, they're bytes, not string data, so you need to convert them into
something that'll fit in a string. That's what encoder.GetString()
does for you.

The Response method would take a string argument, so you need to pass
it the output of the encoder.

s}
Jan 14 '08 #4
OK, thanks.
Another question...

Why is it that I have to hit CR twice to get the response sent to the
term?

this is in a while loop ( while(true) )

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

if (statusMessage.LastIndexOf("\r\n") 0)
{

buffer =
ASCII.GetBytes(ReadResponse(statusMessage));

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

statusMessage = "";

}

and, outside the method for the above code....

protected string ReadResponse(string clientInput)
{
if (clientInput == "hello\r\n")
return "hello, back.";
else
return String.Empty;

}
Jan 15 '08 #5

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

Similar topics

3
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...
4
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
2
by: Bruce W...1 | last post by:
I've got MySQL running as a service on my Windows 2000 box. And I can work with it using a command window (DOS box). I used the default install of MySQL and here's what status says: mysql>...
2
by: thilandeneth | last post by:
i need to do telnet via a web server please give me a idia to initiate the project following requirements are needed 1 Create web based custom telnet client to communicate with remote...
4
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...
13
by: Godzilla | last post by:
Hello, How do you create/spawn new processes in XP over telnet using python? I.e. I would like to create a new process and have it running in the background... when I terminate the telnet...
0
by: pbd22 | last post by:
Hi I am making my way through a windows service that accepts custom telnet commands as strings and then parses the string and redirects it. This would be a custom string sent via telnet. ...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Actually, my installer package is not for a Windows Service, but for a WinForms application. Well, it is kind of both: this is a multi-project solution with its main target being a WinForms...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.