I've attached a file with some code from my app. I hope it helps get you going in the right direction as it has worked for me on windows and linux. There are some comments between sections in the attached code. I've included a method where the TcpListener is instantiated, a MyService base class and a Driver class which inherits from MyService. Look for the ServiceMain method in the Driver class and create your TcpListener therein.
Let me know how it goes,
--Travis
Lee <[EMAIL PROTECTED]> wrote:
Thanks a bunch, Travis.I was wondering...for your socket server, do you use asynchronous sockets? I have tried using a TcpListener from within a service application and the socket server fails to start. I'm wondering what pattern you used for your socket server.Thanks again,Lee
Lee,
From: Travis Staloch [mailto:[EMAIL PROTECTED]
Sent: Friday, June 23, 2006 2:03 AM
To: Lee
Subject: RE: [Mono-list] Auto Running Processeses
Here in the init.d file which I've used to start my mono service. I hope that you're able to get your service working on mono. I know that mine took a little work to get ported over to linux.
I think you just have to put this script in your /etc/init.d folder to get it to run at startup. I might be wrong. Let me know how it goes if you have time.
--Travis
Lee <[EMAIL PROTECTED]> wrote:Travis,Would you mind terribly providing me with a copy of the script that you used? I have tried the script link that you sent me and I can't seem to get it to work.Thanks,Lee
Lee,
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Travis Staloch
Sent: Tuesday, June 20, 2006 1:39 AM
To: [email protected]
Subject: Re: [Mono-list] Auto Running Processeses
I've written a C# TCP socket service which runs on windows and linux. It uses System.ServiceProcess, inheriting from System.ServiceProcess.ServiceBase. The app was originally written on windows and worked fine there installed as a service using installutil. When I moved over to linux, I discovered that I had to start it with MonoService.exe (found in MonoRoot/bin I think).
I had to write an init.d script similar to the one mentioned here in order to get the process launched at OS startup.
--Travis
Lee <[EMAIL PROTECTED]> wrote:
Hi all,
I apologize for the basic question here, but I am new to linux. To make
things a bit easier on myself, I'm using VS2005 for my development since I
use that for windows development as well. I am writing a TCP socket server
and want it to run as an automatic process.
Should that be written as a console app or as a Service app?
Again, sorry for the basic question.
Thanks,
Lee
_______________________________________________
Mono-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list
Want to be your own boss? Learn how on Yahoo! Small Business.
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.1/369 - Release Date: 6/19/2006
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2�/min or less.
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.2/372 - Release Date: 6/21/2006
Sneak preview the all-new Yahoo.com. It's not radically different. Just radically better.
Lee, here's where I use the tcpListener:
/// <summary>
/// Open and listen to a local socket connection
/// </summary>
/// <returns>0 on success</returns>
public int DoPortSources()
{
// for now, just listen to the first port source defined
PortSource portSrc = portSourceList[0];
log.WriteLogEntry("PortSource", "Listening to PortSource: " +
portSrc.Name +
" on hostname: " + portSrc.Hostname + " or ip: " + portSrc.Ip + "
on port: " +
portSrc.Port.ToString(), DebugLevel.Info);
IPAddress ipAddress;
if(portSrc.Hostname != null)
ipAddress = Dns.Resolve(portSrc.Hostname).AddressList[0];
else
ipAddress = IPAddress.Parse(portSrc.Ip);
// start listening to the socket
TcpListener listener = new TcpListener(ipAddress, portSrc.Port);
listener.Start();
// accepting the pending connection request from the WPC
TcpClient socketAccepted = null;
socketAccepted = listener.AcceptTcpClient();
return DoReader(socketAccepted.GetStream());
}
Here's the base class which adds a few thing to ServiceBase:
using System;
using System.Threading;
namespace SportsFeed
{
/// <summary>
/// Summary description for MyService.
/// </summary>
abstract public class MyService : System.ServiceProcess.ServiceBase
{
protected System.DateTime dStartDateTime;
protected TimeSpan tsDelay;
protected Thread m_thread;
protected ManualResetEvent m_shutdownEvent;
protected System.ComponentModel.Container components = null;
protected bool bSignaled = false;
public MyService(string sServiceName)
{
InitializeComponent(sServiceName);
}
protected void InitializeComponent(string sName)
{
components = new System.ComponentModel.Container();
this.ServiceName = sName;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// create our threadstart object to wrap our delegate method
ThreadStart ts = new ThreadStart( this.ServiceMain );
// create the manual reset event and
// set it to an initial state of unsignaled
m_shutdownEvent = new ManualResetEvent(false);
// create the worker thread
m_thread = new Thread( ts );
// go ahead and start the worker thread
m_thread.Start();
base.OnStart(args);
}
protected abstract void ServiceMain();
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
base.OnStop();
//m_thread.Abort();
}
}
}
And here's an example of a driver class which inherits from the MyService
class.
I'm really sorry that this came out with no line breaks. I can't figure out
why it
pasted into wordpad like this. Hoping that it will pretty up when you paste
into your
editor.
using System;
using System.ComponentModel;
using System.Diagnostics;
namespace SportsFeed
{
/// <summary>
/// Used For Debugging
/// </summary>
public class Driver : MyService
{
Tests t;
public static void Main()
{
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
//System.ServiceProcess.ServiceBase[] ServicesToRun;
//ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
Driver() };
//System.ServiceProcess.ServiceBase.Run(ServicesToRun);
// these lines replace the 3 above and allows the service to be
debugged in the ide
Driver DebugService = new Driver();
DebugService.OnStart(null);
}
public Driver(string sServiceName) : base(sServiceName)
{
InitializeComponent(sServiceName);
tsDelay = new TimeSpan(0, 0, 0, 0, 500);//TimeSpan(0, 0, 0, 0, 200);
}
public Driver() : this("Sports Feed - Test Driver") {}
protected override void ServiceMain()
{
try
{
t = new Tests();
t.log.WriteLogEntry("TestDriver", "got to ServiceMain()",
DebugLevel.Info);
t.DirectorySource();
return;
while( true )
{
bSignaled = m_shutdownEvent.WaitOne( tsDelay, true );
// if we were signaled to shutdow, exit the loop
if( bSignaled == true )
break;
}
}
catch(Exception e)
{
EventLogger.WriteError(this, "General error im ServiceMain(): " +
e.ToString());
}
}
}
}
_______________________________________________ Mono-list maillist - [email protected] http://lists.ximian.com/mailman/listinfo/mono-list
