Windows service with sockets

J

jwpaco

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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Sockets;

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()
{
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("IVRSource"))
{

System.Diagnostics.EventLog.CreateEventSource("IVRSource", "IVRLog");
}
eventLog1.Source = "IVRSource";
eventLog1.Log = "IVRLog";
}

protected override void OnStart(string[] args)
{
svcTCP service = new svcTCP();

eventLog1.WriteEntry("Service Started.");

listener = new TcpListener(ip, port);
listener.Start();
}

protected override void OnStop()
{
eventLog1.WriteEntry("Service Stopped.");
}

protected override void OnContinue()
{
eventLog1.WriteEntry("Service Resumed.");
}
}
}


</code>
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 
J

jwpaco

If you have some example code...that would be great.


Justin


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.machin AT dot.state.fl.us
Florida Department Of Transportation


jwpaco said:
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Sockets;

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()
{
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("IVRSource"))
{

System.Diagnostics.EventLog.CreateEventSource("IVRSource", "IVRLog");
}
eventLog1.Source = "IVRSource";
eventLog1.Log = "IVRLog";
}

protected override void OnStart(string[] args)
{
svcTCP service = new svcTCP();

eventLog1.WriteEntry("Service Started.");

listener = new TcpListener(ip, port);
listener.Start();
}

protected override void OnStop()
{
eventLog1.WriteEntry("Service Stopped.");
}

protected override void OnContinue()
{
eventLog1.WriteEntry("Service Resumed.");
}
}
}


</code>
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Here is the code, let me know if you miss something

public class MailGateway : System.ServiceProcess.ServiceBase

{

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.Synchronized( unsyncq);

//Now set the listening part

try

{

TcpClient socket;

TcpListener listener = new TcpListener( Convert.ToInt32(
System.Configuration.ConfigurationSettings.AppSettings["Port"]) );

listener.Start();

while( true)

{


socket = listener.AcceptTcpClient();

//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( TheConnectionHandler));

workingthread.Start();

}

}

catch( Exception e)

{

}

}


public void TheConnectionHandler()

{

TcpClient socket= (TcpClient)connectionQueue.Dequeue();


}

}






--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


jwpaco said:
If you have some example code...that would be great.


Justin


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.machin AT dot.state.fl.us
Florida Department Of Transportation


jwpaco said:
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Sockets;

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()
{
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("IVRSource"))
{

System.Diagnostics.EventLog.CreateEventSource("IVRSource", "IVRLog");
}
eventLog1.Source = "IVRSource";
eventLog1.Log = "IVRLog";
}

protected override void OnStart(string[] args)
{
svcTCP service = new svcTCP();

eventLog1.WriteEntry("Service Started.");

listener = new TcpListener(ip, port);
listener.Start();
}

protected override void OnStop()
{
eventLog1.WriteEntry("Service Stopped.");
}

protected override void OnContinue()
{
eventLog1.WriteEntry("Service Resumed.");
}
}
}


</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top