Solved

How do I write a C# program to run as a service and listen on a specific tcp port for incomming data

Posted on 2006-07-21
2
231 Views
Last Modified: 2010-04-16
Can someone point me in the write direction on how to write a program that will run as a service and then listen on a specific tcp port for data?

Thanks
Kenny
0
Comment
Question by:kwh3856
2 Comments
 
LVL 6

Accepted Solution

by:
NunoGodinho earned 500 total points
ID: 17158576
Hi,

   You have to create a Windows Service that is a class that inherits from System.ServiceProcess.ServiceBase and implement the methods OnStart and OnStop. In order to listen to a specific port you can use .NET Remoting to do this.

   Something like this:

    class Service1 : System.ServiceProcess.ServiceBase
    {
        System.Runtime.Remoting.Channels.Tcp.TcpChannel channel = null;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Runtime.Remoting.Channels.Tcp.TcpChannel channel = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(80);
            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);
        }

        protected override void OnStop()
        {
            if (channel != null)
                System.Runtime.Remoting.Channels.ChannelServices.UnregisterChannel(channel);
        }
    }

 Hope it helped,

   Nuno Godinho
0
 

Author Comment

by:kwh3856
ID: 17158588
Nuno,
Thank you very much.  This is exactly what I need.

Thanks
Kenny
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question