Hi,
Here is the code, let me know if you miss something
public class MailGateway : System.ServiceP rocess.ServiceB ase
{
Thread listenerThread;
Queue connectionQueue = null;
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
listenerThread = new Thread( new ThreadStart( ListenerMethod) );
listenerThread. Start();
}
protected void ListenerMethod( )
{
//In this form we have no control over the children threads
Thread workingthread;
Queue unsyncq = new Queue();
connectionQueue = Queue.Synchroni zed( unsyncq);
//Now set the listening part
try
{
TcpClient socket;
TcpListener listener = new TcpListener( Convert.ToInt32 (
System.Configur ation.Configura tionSettings.Ap pSettings["Port"]) );
listener.Start( );
while( true)
{
socket = listener.Accept TcpClient();
//Now we have to insert the new socket in the queue
connectionQueue .Enqueue( socket);
//create the new thread that will handle this new connection
workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
workingthread.S tart();
}
}
catch( Exception e)
{
}
}
public void TheConnectionHa ndler()
{
TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();
}
}
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"jwpaco" <jw*****@gmail. comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
If you have some example code...that would be great.
Justin
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi,
You cannot block the onStart method, you have to create a new thread in
the
OnStart and then start listening in this new thread.
If you service support more than one client you then need to spawn a new
thread for each connection in this way you can service several clients at
the same time
let me know if you need some code
--
--
Ignacio Machin,
ignacio.mach in AT dot.state.fl.us
Florida Department Of Transportation
"jwpaco" <jw*****@gmail. comwrote in message
news:11******* *************** @q16g2000cwq.go oglegroups.com. ..
>I am new to services and I have quite the task. I need a windows
service that will, once started, open a socket and listen for client
connections. When a connection is made, the client will want the
service to query the db and return a value. This is for use with an IVR
system. Basically, a customer will callin and request some account
information and the service will need to qoery the db and return an
answer to the customer through the IVR system. I have started the
project but I am not getting anywhere fast. I an not even sure I am on
the right path. I thought of using web services, but that is not an
option because the machine this will be installed on will not have IIS
and it cannot be installed. I will paste my code below. Any help is
appreciated.
Justin
<code>
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;
using System.Net;
using System.Net.Sock ets;
namespace MyIVR
{
public partial class svcTCP : ServiceBase
{
private readonly int port = 45000;
private readonly IPAddress ip = IPAddress.Parse ("127.0.0.1" );
private TcpListener listener;
public svcTCP()
{
InitializeCompo nent();
if (!System.Diagno stics.EventLog. SourceExists("I VRSource"))
{
System.Diagnost ics.EventLog.Cr eateEventSource ("IVRSource" , "IVRLog");
}
eventLog1.Sourc e = "IVRSource" ;
eventLog1.Log = "IVRLog";
}
protected override void OnStart(string[] args)
{
svcTCP service = new svcTCP();
eventLog1.Write Entry("Service Started.");
listener = new TcpListener(ip, port);
listener.Start( );
}
protected override void OnStop()
{
eventLog1.Write Entry("Service Stopped.");
}
protected override void OnContinue()
{
eventLog1.Write Entry("Service Resumed.");
}
}
}
</code>