How to create a server on Windows

I am asking this question as a guy with strong Linux programming. I have very little experience with Windows development and so far this has been limited to programs like "Console Application".

What is the canonical process for developing a server knowing it should work on Windows?

Is the Windows Service app running right now? Is the process of debugging as frustrating as it sounds ? Does a Windows developer start with a command line app for debugging and only convert it to a service for deployment?

For comparison, I suggest that the Linux server be a regular command line in the language of your choice. I define the server as a program that is stereotyped,

  • produces most of its human-readable output as logs,
  • accepts input from the operating system (such as network or file system events), as opposed to "user pressed a button" or "user typed a command" and
  • used as a demon.

Perhaps Windows sysadmins expects completely different interfaces for starting, stopping, and managing servers. I am absolutely open to this; I haven't seen this side yet.

$ ./my-server &
Starting...
$ head /var/my-server.log
2012-3-10 14:34:43.934 [info] Server started! Waiting for connection from client
$ 

      

+3


source to share


2 answers


First: yes, it is almost always preferable for the Windows server to be implemented as a system service. However, it's an added bonus if the server can also be started from the command line for troubleshooting.

It is generally not too difficult to structure your code so that service-specific logic is abstracted into functions that can check if you are running as a service or on the command line and behave accordingly. I prefer to have a single source module containing only the main () function and service-specific stuff. For best results, make sure that only this module knows which mode you are operating in. (Note also that this module can often be inherited from one project to another with minor changes.)



Another advantage of this is that it means you can do most of your debugging in command line mode without having multiple build options. You will still have to test (and possibly debug) the service logic itself, but this is much less.

There is more information here that you have probably seen already.

+3


source


On Windows, a service is something that

  • produces most of its human-readable output in event forms sent to the event log;
  • accepts input from the operating system through any events it likes to respond to, but must always respond to requests from the Service Control Manager (SCM) (for example, "starting a service" "stop", "pause", ...).

    Exactly how these events are delivered depends on the capabilities of your chosen programming language; in C (that is, at the OS level), you need to set up an event loop and view the events received. You usually do the actual work in separate threads with the main thread of the event loop.

  • Deployed as a service, which is usually a console executable that is registered with the SCM.



For more information on services in .NET see here ; for full details on how it all works at the C level (i.e. without any fancy language features) see here .

+2


source







All Articles