473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2847
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
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,...
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...
1
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...

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.