[Mono-list] TCPListener Service application
Lee Jenkins (DataTrakPOS)
lee.jenkins at datatrakpos.com
Tue Jun 20 19:17:56 EDT 2006
Hi, I have written a prototype TCPListerner service. It works fine on my
local XP dev box (isn't that always the case), but not on the linux box.
I started the service with:
$ Mono-service myservice.exe
Checked with ps and ps -eaf and it says that its running. So at this point,
I'm not sure if it's the linux box or something in my code that does not run
well in mono.
Is there anything in particular I should try? I have attached the code that
starts the service below.
Thanks,
--
Lee
protected override void OnStart(string[] args)
{
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(this.StartServe));
thread.Start();
}
private void StartServe()
{
TcpListener SockServer = null;
try
{
// Setup socket server
Int32 port = 4567;
SockServer = new TcpListener(IPAddress.Any, port);
SockServer.Start();
// create buffer to read data
Byte[] bytes = new Byte[256];
string data = null;
while (true)
{
// check to see if request is pending
if (SockServer.Pending())
{
TcpClient client = SockServer.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
// flag for loop
bool Alldone = false;
// End of Transmission character
char theChar = '\u0004';
string ch = theChar.ToString();
while (!Alldone)
{
i = stream.Read(bytes, 0, bytes.Length);
// translate stream to string data;
data += System.Text.Encoding.ASCII.GetString(bytes);
if ((data.Contains(ch)) || (i == 0))
Alldone = true;
}
int POS = data.IndexOf(ch);
data = data.Substring(0, POS);
// strip off EOT character
// create requestprocessing object here and hand off
RequestHandler handler = new RequestHandler(data);
string response = handler.ProcessRequest() + ch;
// send back result
byte[] ret = System.Text.Encoding.ASCII.GetBytes(response);
stream.Write(ret, 0, ret.Length);
}
}
}
catch (SocketException e)
{
}
finally
{
SockServer.Stop();
}
More information about the Mono-list
mailing list