Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A TCP/IP Server written in C#

, 29 Dec 2003
TCP/IP Server, which is running as a Windows Service waiting for client requests
client.zip
TCPClient
App.ico
TCPClient.csproj.user
tcpserver.zip
TCPServiceSetup
TCPServiceSetup.vdproj
vssver.scc
TCPService
TCPService.csproj.user
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace TCPService
{
	/// <summary>
	/// Class that will run as a Windows Service and its display name is
	/// TCP (Sabre Group Config Transfer Service) in Windows Services.
	/// This service basically start a server on service start 
	/// (on OnStart method) and shutdown the server on the servie stop 
	/// (on OnStop method).
	/// </summary>
	public class TCPService : System.ServiceProcess.ServiceBase
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private TCPServer server=null;

		public TCPService()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();
		}

		// The main entry point for the process
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
	
			// More than one user Service may run within the same process. To add
			// another service to this process, change the following line to
			// create a second service object. For example,
			//
			//   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new TCPService(), new MySecondUserService()};
			//
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new TCPService() };

			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
			this.ServiceName = "TCPService";
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			// Create the Server Object ans Start it.
			server = new TCPServer();
			server.StartServer();
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			// Stop the Server. Release it.
			server.StopServer();
			server=null;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Fact Pandit
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 30 Dec 2003
Article Copyright 2003 by Fact Pandit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid