Creating a Windows Service that runs a TCP/IP Server

this tutorial demonstrates creating a windows service and a simple windows sockets based TCP/IP server


Introduction

Intended Audience

This tutorial is intended for programmers that have a basic understandin of windows programming and some knowledge of C or C++.

It is assumed that the reader will be familiar with the C programming language and syntax but knowledge of complex software patterns or constructs will not be required.

Also, the reader is expected to be somewhat familiar with windows programming. It is not necessary to know the APIs used in this article but an understanding of basic concepts used in C and C++ based winows programming will be advantageous.

The samples are written for, and tested with the Microsoft Visual C++ 6.0 compiler and IDE although one should be able to compile them with other C++ compilers. Hoewver this tutorial does not provide any directions on how to do so.

Create a New Project

Open the Visual Studio IDE, and from the "File" menu slect "New.." (or press Alt-N). This will open the "Select New Project Screen" (see below). From the Projects tab, select Win32 Console Application and give it a name in the "Poject name" text box.

Select New Project Screenshot

When you click OK you will be prompted to configure your new application. We will be starting our development from scratch so you can select "A simple application" from the options and then click Finish. Visual Studio will create the project files and a source code file for us to start working in. This is useful.

Select Project Options Screenshot

Setting Up The Precompiled Header

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include "windows.h"
#include "process.h"
#include "winsvc.h"
#include "winsock2.h"
#include "time.h"
#include "stdio.h"
#include "malloc.h"

copyright (c) Miky Dinescu 2007 - 2008

500GB space, 5TB bandwidth, only $5.95/mo - Dreamhost.Com Green Web Hosting! This site hosted by DreamHost.