473,651 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows service with sockets

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>

Aug 10 '06 #1
3 10341
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" <jw*****@gmail. comwrote in message
news:11******** **************@ q16g2000cwq.goo glegroups.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>

Aug 10 '06 #2
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.machin AT dot.state.fl.us
Florida Department Of Transportation
"jwpaco" <jw*****@gmail. comwrote in message
news:11******** **************@ q16g2000cwq.goo glegroups.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>
Aug 11 '06 #3
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>

Aug 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2485
by: kshetgar | last post by:
I am experiencing a wierd problem... I have a C#.Net application Server which uses Sockets. It listens on port 11000. and it runs as a Windows Service. If I run it as a consple Application, I can connect to that port/Application from any machine on the network as well as from my local machine.
0
1165
by: Kumar Shetgar | last post by:
I am experiencing a wierd problem... I have a C#.Net application Server which uses Sockets. It listens on port 11000. and it runs as a Windows Service. -- If I run it as a consple Application, I can connect to that port/Application from any machine on the network as well as from my local machine.
2
2678
by: domtam | last post by:
Hi there. My goal is to write a windows service that can act as HTTP Request server. How can I do that? I know that I can use ASP.NET to develop a web site to achieve this purpose, i.e. receive HTTP request. However, I'd like it to exist in the form of a "Windows Service".
8
1813
by: Mike C# | last post by:
Anyone know of any good articles on how to write a windows service in native (unmanaged) VC++? Also, how to use IPC in the service to communicate with a front-end GUI application would be helpful... Thanks!
2
2826
by: =?Utf-8?B?Sm9obiBG?= | last post by:
Hello All, I have a question about using an ActiveX control with a Windows Servce in C#. I believe it is not possible to properly setup an ActiveX control in a windows service as the particular ActiveX control we're using (GrFinger for fingerprint reader)implements several event handlers. It is also my understanding that there is no Message Pump within a Windows Service. I suppose I could create my own message pump, but this seems...
7
10860
by: GD | last post by:
Hi, I am trying to call a webservice from a windows service application. It works only if I launch the windows service app from VS.Net 2005 (Worked around from Main()) or from a winform test application. However, it generates a kind of security error after I install and start the service in my Window Server machine. I believe that it is related to authentication. The following is the sample code: HttpWebRequest obj =...
1
2869
by: mdhaman | last post by:
hi, I have a windows service written in VB.Net and framework 2.0. It is a multithread service and it is using threadpool to manage threads. Recently I have started getting NullReferenceException and after that windows service crashes. System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"
2
3590
by: =?Utf-8?B?U2Vhbk1hYw==?= | last post by:
I am familiar with how to use winsock in vb6 to create a network app. I'm trying to find a way to take the current vb6 app and create a windows service using system.net.sockets. How do you create a service (for the server side) that accepts incoming client connections using system.net.sockets? -- Sean McIntire
7
1896
by: bkkirankumar | last post by:
Hi, I had used Sockets in my windows service which is being developed in C#. Earlier I wrote an application and I converted that to service. But the sockets are being blocked(or) not initializing. I am using this to receive multicast streams. Please let me know for any suggestions or answers. Thanks in advance Black
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.