?
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
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
NunoGodinho earned 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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