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();
}