Click here to Skip to main content
14,934,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a windows service in C#. I followed some existing tutorials from google. In service.cs class the constructor is only holding the InitializeComponent() method. In OnStart() method I have written some code to open a port using TCPClient to connect via tcp connection to some other client. In that OnStart() method I am receiving a response from client using AcceptTcpClient() method and forwarding to an another class to process the client's response and send it back to the client. I maintained the coding fashion in ServiceInstaller and ServiceProcessInstaller and to start the service automatically adding up the event handler named AfterInstall and Commited. As I am installing the service, in service.msc it is showing service status as starting but the service is working as it should. Why the status is not showing running state?

What I have tried:

C++
public partial class WinService: ServiceBase
{
      private static TCPListen listener = null;

      public Winservice()
      {
            InitializeComponent();
      }
      
      protected override void OnStart(String[] args)
      {
            loadconfig(); // Loading configuration data variables like port number IPAddress
            listener = new TcpListener(IPAddress, PortNumber);
            listener.Start();
            AnotherClass handler = new AnotherClass(listener.AcceptTcpClient);
            Thread thd = new Thread(new ThreadStart(handler.HandleSession()));
            thd.Start();
      }

      protected override void OnStop()
      {
            listener.Stop();
      }

      public loadconfig()
      { Method implementation }
}
Posted
Updated 22-Aug-18 0:03am
v2

1 solution

TcpListener.AcceptTcpClient() is a blocking call. As a result, your OnStart() method will only return when a client has connected. But the start routine of a service should return as soon possible. If it runs for a too long time, the service control manager will give up waiting for the function to return letting the status unchanged.

The solution is to execute the Accept call within an own thread.
   
Comments
Pal Sayantan 22-Aug-18 6:24am
   
I am very beginner with all this stuffs. Can you give me a snippet please? How to Accept call within an own thread.
Jochen Arndt 22-Aug-18 7:01am
   
You already created a thread. Just pass the TcpListener to your class instead of the TcpClient returned by the accept call.

The basic handling of listening sockets is the same as for normal applications which should use a thread too. The thread function is usually a while loop that waits for incoming connections and handles them. It must check also for a termination condition which should be fired in your case when stopping the service (already done by stopping the listener).

Such should be covered by every tutorial about TCP listening. I suggest to read some because the provided code has been tested while I would have to write it from scratch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900