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
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?

Question by:kwh3856

Accepted Solution

NunoGodinho earned 500 total points
ID: 17158576

   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()

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

        protected override void OnStop()
            if (channel != null)

 Hope it helped,

   Nuno Godinho

Author Comment

ID: 17158588
Thank you very much.  This is exactly what I need.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to error 4 44
scanning dentists xray (the small ones) 3 44
C#  Radio button search for Date not DateTime 4 35
C# parent child form 5 17
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

23 Experts available now in Live!

Get 1:1 Help Now