D
Dan Holmes
The following is the start of an application that will eventually run as
a service. It listens on port 23 and emulates VT100. I would like some
feedback on the network stream reading and writing. I don't really know
if this is the correct approach for what i am trying.
I have telnet clients that i am controlling. I don't want the
processing of the input of one terminal to stop another terminal from
processing.
I think i have things right up until the ProcessMessageFromTerminal method.
any input is greatly appreciated. Also any links or books on this
subject would be handy as well.
dan
using System;
using System.Collections.Generic;
using System.Text;
using SNS = System.Net.Sockets;
namespace IVS.Framework.RF
{
class Multiplexor
{
static class VT100Commands
{
public static const string esc = 0x1b;
public static const string CLRSCRN = esc + "[2J";
public static const string REVERSE = esc + "[7m";
public static const string CLREOL = esc + "[K";
public static const string CLREOS = esc + "[J";
public static const string REGULAR = esc + "[0m";
public static const string HOME = esc + "[H";
public static const string LOCKKBD = esc + "[2h";
public static const string UNLOCKKBD = esc + "[2l";
public static const string UNDERLINE = esc + "[4m";
}
List<RFProcess> rfProcesses = null;
public Multiplexor()
{
rfProcesses = new List<RFProcess>();
}
static void Main(string[] args)
{
Multiplexor mux = new Multiplexor();
WriteUsage();
SNS.TcpListener listn = new
SNS.TcpListener(System.Net.IPAddress.Any, 23);
listn.Start();
IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(mux.ProcessSocket), listn);
bool keepGoing = true;
do
{
ConsoleKeyInfo c = System.Console.ReadKey();
switch (c.Key)
{
case ConsoleKey.X:
keepGoing = false;
break;
}
} while (keepGoing);
}
static void WriteUsage()
{
Console.WriteLine("Press 'x' to close");
}
private void ProcessSocket(IAsyncResult ar)
{
RFProcess rf = new RFProcess();
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
rf.Socket = listener.EndAcceptTcpClient(ar);
listener.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listener);
Console.WriteLine("Client connected completed");
WriteToClient(rf.Socket, VT100Commands.CLRSCRN +
"connecting...");
lock (rfProcesses)
{
rfProcesses.Add(rf);
}
}
public static void WriteToClient(SNS.TcpClient client, string
message)
{
client.GetStream().Write(Encoding.ASCII.GetBytes(message),
0, message.Length);
}
}
class RFProcess
{
const int BufferSize = 512;
byte[] recvBuffer = new byte[BufferSize];
StringBuilder buffer = new StringBuilder();
SNS.TcpClient _socket = null;
public SNS.TcpClient Socket
{
get { return _socket; }
set {
_socket = value;
_socket.GetStream().BeginRead(recvBuffer, 0,
recvBuffer.Length
, new AsyncCallback(ProcessMessageFromTerminal), this);
}
}
private void ProcessMessageFromTerminal(IAsyncResult ar)
{
RFProcess me = (RFProcess)ar.AsyncState;
SNS.NetworkStream ns = me.Socket.GetStream();
int bytesRead = ns.EndRead(ar);
buffer.Append(Encoding.ASCII.GetString(readBuffer, 0,
bytesRead));
//this is currently an echo. i will replace this will application
logic later.
Multiplexor.WriteToClient(me.Socket, buffer.ToString());
ns.BeginRead(recvBuffer, 0, recvBuffer.Length
, new AsyncCallback(ProcessMessageFromTerminal), me);
}
}
}
a service. It listens on port 23 and emulates VT100. I would like some
feedback on the network stream reading and writing. I don't really know
if this is the correct approach for what i am trying.
I have telnet clients that i am controlling. I don't want the
processing of the input of one terminal to stop another terminal from
processing.
I think i have things right up until the ProcessMessageFromTerminal method.
any input is greatly appreciated. Also any links or books on this
subject would be handy as well.
dan
using System;
using System.Collections.Generic;
using System.Text;
using SNS = System.Net.Sockets;
namespace IVS.Framework.RF
{
class Multiplexor
{
static class VT100Commands
{
public static const string esc = 0x1b;
public static const string CLRSCRN = esc + "[2J";
public static const string REVERSE = esc + "[7m";
public static const string CLREOL = esc + "[K";
public static const string CLREOS = esc + "[J";
public static const string REGULAR = esc + "[0m";
public static const string HOME = esc + "[H";
public static const string LOCKKBD = esc + "[2h";
public static const string UNLOCKKBD = esc + "[2l";
public static const string UNDERLINE = esc + "[4m";
}
List<RFProcess> rfProcesses = null;
public Multiplexor()
{
rfProcesses = new List<RFProcess>();
}
static void Main(string[] args)
{
Multiplexor mux = new Multiplexor();
WriteUsage();
SNS.TcpListener listn = new
SNS.TcpListener(System.Net.IPAddress.Any, 23);
listn.Start();
IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(mux.ProcessSocket), listn);
bool keepGoing = true;
do
{
ConsoleKeyInfo c = System.Console.ReadKey();
switch (c.Key)
{
case ConsoleKey.X:
keepGoing = false;
break;
}
} while (keepGoing);
}
static void WriteUsage()
{
Console.WriteLine("Press 'x' to close");
}
private void ProcessSocket(IAsyncResult ar)
{
RFProcess rf = new RFProcess();
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
rf.Socket = listener.EndAcceptTcpClient(ar);
listener.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listener);
Console.WriteLine("Client connected completed");
WriteToClient(rf.Socket, VT100Commands.CLRSCRN +
"connecting...");
lock (rfProcesses)
{
rfProcesses.Add(rf);
}
}
public static void WriteToClient(SNS.TcpClient client, string
message)
{
client.GetStream().Write(Encoding.ASCII.GetBytes(message),
0, message.Length);
}
}
class RFProcess
{
const int BufferSize = 512;
byte[] recvBuffer = new byte[BufferSize];
StringBuilder buffer = new StringBuilder();
SNS.TcpClient _socket = null;
public SNS.TcpClient Socket
{
get { return _socket; }
set {
_socket = value;
_socket.GetStream().BeginRead(recvBuffer, 0,
recvBuffer.Length
, new AsyncCallback(ProcessMessageFromTerminal), this);
}
}
private void ProcessMessageFromTerminal(IAsyncResult ar)
{
RFProcess me = (RFProcess)ar.AsyncState;
SNS.NetworkStream ns = me.Socket.GetStream();
int bytesRead = ns.EndRead(ar);
buffer.Append(Encoding.ASCII.GetString(readBuffer, 0,
bytesRead));
//this is currently an echo. i will replace this will application
logic later.
Multiplexor.WriteToClient(me.Socket, buffer.ToString());
ns.BeginRead(recvBuffer, 0, recvBuffer.Length
, new AsyncCallback(ProcessMessageFromTerminal), me);
}
}
}