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
205 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
Comment Utility
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
Comment Utility
Nuno,
Thank you very much.  This is exactly what I need.

Thanks
Kenny
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now