[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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